This article demonstrates the standard size of int and long data types that are widely used in C++ programming.
What is an int Data Type in C++
The int is a data type in C++ that is utilized to store integer numbers. It can store both signed and unsigned integers. The signed integers could be positive or negative, while unsigned integers are always positive. Whether it can express negative numbers depends on whether it is signed or unsigned.
Standard State Size of an int in C++
The standard state size of an int in C++ is 4 bytes (32 bits). It can store values that range start from -2,147,483,648 and end up to 2,147,483,647. However, the size of the int may vary depending on the compiler or operating system you are using. Like, for 32Bit OS, the size of int is 4 bytes but for 64Bit OS, it could be 8 bytes (64 bits). Still, on most compilers, the standard size of int is set to 4 bytes.
What is meant by long Data Type in C++
Long data type in C++ stores values for variables or constants with 64 bits of storage and is a signed integer that is used to store values for variables or constants that are greater than the usual number 32-bit data type.
Standard State Size of long in C++
On most compilers, the standard size of long in C++ is 8 bytes (64 bits). However, the size can vary if you use the long data type in an operating system of 32Bit. The reason to set the size of long to 8 bytes is the fact that users can store much larger values as compared to int. It means that if users want to work on big numbers, they can prefer to use long instead of int in C++ programs.
How to Calculate a Data type’s Size in C++
If you are using a compiler on any operating system, you can find the size of int and long using the following C++ code.
using namespace std;
int main() {
cout << "Total-size of int: " << sizeof (int) << endl;
cout << "Total-size of long: " << sizeof (long) << endl;
cout << "Total-size of long int: " << sizeof (long int) << endl;
return 0;
}
In the above code, you can find the size of an int, long and long int.
Output
Conclusion
In C++, the values are stored in data types called integer and long-data type variables in the memory. They have some standard values with respect to the compiler size. But in most cases, the int datatype has a standard size of 4 bytes and a long data type has a size of 8 bytes in C++ language.