I've been programming in C++ for quite a while now and I am pretty familiar with most of the stuff. One thing that I've never understood though is the 'long' data type.
I googled it but I still don't know what it is for. I've found pages that say it is the same size and has the same range as an int. So what would be the point in using it?
I found another stack overflow question regarding this here: Difference between long and int data types
And it seems that the only difference between the two is that sometimes the size is different on different systems. Does that mean that an application that uses long on a 64bit machine won't work on a 32bit machine? If so then wouldn't it be better to not use them at all?
Also I noticed stuff called "long int" or even "long long"! Is it a data type or a modifier?
The long data type is used when you need a range of values more than those provided by int. Example: long a = 100000L, long b = -200000L.
Longer integers: long The long data type stores integers like int , but gives a wider range of values at the cost of taking more memory. Long stores at least 32 bits, giving it a range of -2,147,483,648 to 2,147,483,647. Alternatively, use unsigned long for a range of 0 to 4,294,967,295.
long Type Modifier If we need to store a large integer (in the range -2147483647 to 2147483647), we can use the type specifier long . For example, // large integer long b = 123456; Note: long is equivalent to long int .
It is compiler dependent. My standards-fu is a bit rusty but I believe it is defined as follows:
char <= short <= int <= long <= long long
where:
char >= 8 bits
short >= 16 bits
int >= 16 bits
long >= 32 bits
long long >= 64 bits
Which means that it is perfectly valid to have char = short = int = long = long long = 64bits
and in fact compilers of some DSPs are designed that way.
This underscores the importance of actually reading your compiler documentation.
I noticed stuff called "long int" or even "long long"! Is it a data type or a modifier?
long int
is the same as long
(just as short int
is the same as short
).
long long
is a distinct data type introduced by several compilers and adopted by C++0x.
Note that there is no such thing as long long long
:
error: 'long long long' is too long for GCC
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With