Long Int C++

Community Forums/General Help/Long Int C++

Yue(Posted 2011) [#1]
Hi I have a concern regarding the data types declared in C + +, some are integers (int) and others are long integers (long), which can not understand is why they have the same range capability 2,147,483,648 <= X <= 2,147,483,647 I hoped that at long range (long) was more something that int (int), I appreciate the help here.


Yasha(Posted 2011) [#2]
"int" is a convenience name for the most common type of integer on your system. It matches the size of a memory address. "long" is another convenience name for the middle size of the standard integers (alongside "short" and "long long"). It is usually, but not always, the same size as an "int".

The sizes for all four of these names aren't guaranteed to be the same on all platforms, so they're chosen to be whatever the compiler author thinks will work most "naturally". The notable point is that the "natural" size of an int is 32-bit on Windows and 32-bit Linux, but 64-bit on 64-bit Linux (or something like that).

If you really need a specific range and don't want it to potentially vary if the program is recompiled on another machine, you can use the specific names of each size of integer, which are defined in stdint.h: int32_t will give you a 32-bit int everywhere it's available, int64_t will always give you a 64-bit integer.

Unless you're really concerned that you might not get the right size (which is only an issue if you intend your code to be multiplatform anyway), it's easier (much more readable) to just learn what the conventional names are for the numbers on your OS, and go with those.

Last edited 2011


*(Posted 2011) [#3]
I do think some compilers have them for compatibility with older source code.