Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 685
Alex Avatar asked Dec 01 '10 21:12

Alex


People also ask

What is long data type example?

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.

What is the use of long data type in C?

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.

What is the purpose of a long in C++?

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 .


2 Answers

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.

like image 157
slebetman Avatar answered Oct 10 '22 20:10

slebetman


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
like image 21
fredoverflow Avatar answered Oct 10 '22 21:10

fredoverflow