C++ Program to Convert long Type Variables to int


C++ has support for various different datatypes to accommodate the different representations and sizes of data. The datatypes that can be used to represent numerical values in C++ are int, long, float, and double. int and long are used to represent integer values, whereas float and double are used to represent fractional values. Both int and long contains integer values with one difference which is, int is of size 4 bytes and long is of size 8 bytes.

Conversion of long to int can be done in two different ways, one is through implicit conversion and the another one is using explicit conversion. Explicit type conversion requires mentioning the resultant datatype in the code and implicit type conversion is done by the compiler itself. Explicit type conversion can be further done in two ways which are described further. First, the implicit conversion is described.

Syntax

Implicit conversion of a long type variable to int −

long a;
int b = a;

Implicit conversion is handled fully by the compiler, and the programmer doesn’t have to put any extra effort for the conversion. The source variable only has to be assigned to the destination variable.

Algorithm

  • Take input in a long variable, for example var2.
  • Assign the long variable to an int variable, for example var1.
  • Print the value.

Example

#include <iostream> using namespace std; int solve(long var2) { return var2; } int main() { int var1; long var2 = 1034; var1 = solve(var2); cout<< "The value of var1 is: "<< var1 << endl; cout<< "The size of var1 is: " << sizeof(var1) << endl; cout<< "The size of var2 is: " << sizeof(var2) << endl; return 0; }

Output

The value of var1 is: 1034
The size of var1 is: 4
The size of var2 is: 8

In the example, it can be seen that the variable var1 is of int type and the variable var2 is of long type. The variable var2 is initialized with the value 1034, and it is assigned to var1. It can be seen that an implicit type conversion is done by the compiler and the value is stored as an integer in the variable var1. The sizes of the variables are also displayed in the output to verify the conversion, though it is for demonstration purposes only and does not need to applied every time.

Explicit type conversion in C++ can be done in two ways; one is through using the cast operator, and the another one is through mentioning the data type of the resultant variable while assignment. Let’s start with the cast operator.

Using cast operator

There are four different types of cast operators that are available in C++. In this article, we only use the static_cast operator.

Syntax

long a;
int b = static_cast<int> (a);

Algorithm

  • Take input in a long variable, for example var2.
  • Assign the long variable to an int variable (for example var1) using the static_cast operator.
  • Print the value.

Example

#include <iostream> using namespace std; int solve(long var2) { return static_cast<int>(var2); } int main() { int var1; long var2 = 4031; var1 = solve(var2); cout<< "The value of var1 is: "<< var1 <<endl; cout<< "The size of var1 is: " << sizeof(var1) << endl; cout<< "The size of var2 is: " << sizeof(var2) << endl; return 0; }

Output

The value of var1 is: 4031
The size of var1 is: 4
The size of var2 is: 8

The output is similar to the previous code snippet tried before. The same thing is being done in the explicit type conversion; except this time the user has to mention the conversion process.

Mentioning the data type while assignment

Similar to the previous method, we also mention the resultant data type before the source variable in right hand side while conversion. In this case, no extra operator is needed.

Syntax

long a;
int b = (int) a;

Algorithm

  • Take input in a long variable, for example var2
  • Assign the long variable to an int variable (for example var1) mentioning the resultant data type.
  • Print the value.

Example

#include <iostream> using namespace std; int solve(long var2) { return (int)var2; } int main() { int var1; long var2 = 3104; var1 = solve(var2); cout<< "The value of var1 is: "<< var1 << endl; cout<< "The size of var1 is: " << sizeof(var1) << endl; cout<< "The size of var2 is: " << sizeof(var2) << endl; return 0; }

Output

The value of var1 is: 3104
The size of var1 is: 4
The size of var2 is: 8

This particular conversion method is also known as the ‘C-Styled casting/conversion’ because it is how explicit type conversions are done in C. It also works in C++; we can see from the results.

Conclusion

Conversion between different types of variables is very common in C++ or any programming language as different data types provide different ways to represent and manipulate the same kind of data. We mainly use two types of conversions, which are implicit and explicit types of conversions to convert between long and int. However, int is of size 4 bytes and long is of size 8 bytes and due to this conversion of values greater than the maximum permissible integer value will lead to erroneous conversion of data as well as data loss. So, conversions between data types are to be done carefully.

Updated on: 19-Oct-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements