Home  >  Article  >  Backend Development  >  C++ program to convert int type variable to long type

C++ program to convert int type variable to long type

PHPz
PHPzforward
2023-09-05 19:33:061489browse

C++ program to convert int type variable to long type

C is a statically typed language, so all its variables need to declare data types in advance. A data type indicates what type of value a variable contains. In C, variables that contain numeric values ​​are int, long, float, and double. int and long are used to represent integer values, while float and double are used to represent decimal values. Both int and long contain integer values, but with one difference, the size of int is 4 bytes and the size of long is 8 bytes.

Conversion from int to long can be done in a number of different ways, we've only discussed two of them. The first is through implicit conversion and the second is using explicit conversion. Explicit type conversion requires mentioning the resulting data type in the code, implicit type conversion is done by the compiler itself. Explicit type conversion can be further accomplished in two ways described later. We described implicit conversions before.

grammar

Implicit conversion of int type variable to long -

int a;
long b = a;

Implicit conversions are completely handled by the compiler and the programmer does not need to put in any extra effort for the conversion. Just assign the source variable to the target variable.

algorithm

  • Get the input in an int variable, such as a.
  • Assign int variable to long variable. If the long variable is b, then a is assigned 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 shown in the example, the variable var1 is of type int, while the variable var2 is of type long. The value 4103 is used to initialize variable var2 and then assign it to var1. It's obvious that the compiler performed an implicit type conversion and saved the value as an integer in the variable var1. The variable size is also shown in the output to check the conversion, although this is for demonstration purposes only and does not need to be applied every time. In C, there are two ways to perform explicit type conversions: applying a cast operator and explicitly declaring the data type of the result variable during assignment.

Use cast operator

In C, there are four different types of cast operators available. The static cast operator is the only operator used in this article.

grammar

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

algorithm

  • Get the input in an int variable, such as a.
  • Use the static_cast operator to assign an int variable to a long variable (such as b).
  • 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 results are comparable to previously attempted code snippets. Explicit type conversion accomplishes the same thing, except that the user now needs to specify the conversion method.

Mention the data type when assigning value

Similar to the previous method, when converting, we also put the result data type before the source variable on the right. No additional operator is required in this case.

grammar

int a;
long b = (long) a;

algorithm

  • Get the input in an int variable, such as a.
  • Assign the int variable to a long variable (such as b) and indicate the result 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

in conclusion

Because different data types provide different representations and methods for operating on the same type of data, conversion between them is very common in C and other computer languages. To convert between long and int we mainly use two types of conversions called implicit conversions and explicit conversion types. This conversion makes sense when int type data is required for larger, more complex calculations.

The above is the detailed content of C++ program to convert int type variable to long type. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete