# C++ Program to Convert int Type Variables to long

C++ is a statically typed language, and because of that all of its variables require the datatype to be declared beforehand. The datatypes are used to represent which type of value is contained inside a variable. In C++, the variables to contain numerical values 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 int to long can be done in various different ways, out of them we have dicussed only two. The first one is through implicit conversion and the second 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 later. We describe the implicit conversion before.

## Syntax

Implicit conversion of a int type variable to long −

int a;
long 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 int variable, for example a.
• Assign the int variable to a long variable. If the long variable is b, assign a to b .
• Print the value.

## Example

#include <iostream>
using namespace std;

long solve(int var2) {
return var2;
}

int main()
{
long var1;
int var2 = 4103;
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: 4103
The size of var1 is: 8
The size of var2 is: 4


As seen in the example, variable var1 is of type int, while variable var2 is of type long. The value 4103 is used to initialise the variable var2, which is then assigned to var1. It is clear that the compiler performed an implicit type conversion, and the value was saved as an integer in the variable var1. The variable sizes are also displayed in the output to check the conversion, albeit this is merely for demonstration purposes and does not need to be applied every time. In C++, there are two ways to perform explicit type conversion: applying the cast operator and explicitly stating the data type of the resulting variable when assigning.

## Using cast operator

In C++, there are four different kinds of cast operators available. The static cast operator is the only one utilised in this article.

### Syntax

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


### Algorithm

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

### Example

#include <iostream>
using namespace std;

long solve(int var2) {
return static_cast<long> (var2);
}

int main()
{
long var1;
int var2 = 257;
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: 257
The size of var1 is: 8
The size of var2 is: 4


The result is comparable to the earlier code snippet that was attempted. The explicit type conversion accomplishes the same thing, with the exception that the user now needs to specify the conversion method.

## Mentioning the data type while assignment

Similar to the previous approach, while converting, we also place the resultant data type before the source variable on the right side. No additional operator is required in this scenario.

### Syntax

int a;
long b = (long) a;


### Algorithm

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

### Example

#include <iostream>
using namespace std;

long solve(int var2) {
return (long) var2;
}

int main()
{
long var1;
int var2 = 37857;
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: 37857
The size of var1 is: 8
The size of var2 is: 4


## Conclusion

Since different data types offer various representations and methods for manipulating the same kind of data, conversions between them are highly common in C++ and other computer languages. To convert between long and int, we mostly employ two sorts of conversions, known as implicit and explicit types of conversions. The conversions are meaningful when int type data is needed for more larger and complex calculations.