What's the 'long' data type used for?

Alex picture Alex · Dec 1, 2010 · Viewed 23.4k times · Source

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?

Answer

slebetman picture slebetman · Dec 1, 2010

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.