Alex
Alex

Reputation: 241

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?

Upvotes: 17

Views: 31246

Answers (7)

pythonic metaphor
pythonic metaphor

Reputation: 10556

If you want an integer that is guaranteed to be 32 bit or 64 bit, there are such types, e.g. int64_t. If you really want to ensure your int are of such a size, use these types instead of long, long long, etc. You'll need to include cstdint for these (stdint.h in C).

Upvotes: 0

dan04
dan04

Reputation: 91227

I googled it but I still don't know what its for. I've found pages that say its the same size and has the same range as an int. So what would be the point in using it?

I've wondered the same thing. And concluded that long is now useless.

Prior to the rise of 64-bit systems, the de facto standard for C integer types was:

  • char = (u)int8_t (Note that C predates Unicode.)
  • short = int16_t
  • int = intptr_t [until 64-bit], int_fast16_t
  • long = int32_t [until 64-bit], intmax_t [until 1999]
  • long long = int64_t or intmax_t

Today, however, long has no consistent semantics.

Upvotes: 0

slebetman
slebetman

Reputation: 114024

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.

Upvotes: 29

icecrime
icecrime

Reputation: 76835

This is what the C++03 standard says (3.9.1/2) :

There are four signed integer types: “signed char”, “short int”, “int”, and “long int.” In this list, each type provides at least as much storage as those preceding it in the list.

So : sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)

This is what the C++0x (3.9.1/2) and C99 (6.2.5/4) standards say :

There are five standard signed integer types, designated as signed char, short int, int, long int, and long long int.

  • long is synonym of long int
  • long long doesn't exist in C++03, but will in C++0x.

Upvotes: 4

fredoverflow
fredoverflow

Reputation: 263360

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

Upvotes: 22

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145457

long is guaranteed (at least) 32 bits. int is only guaranteed (at least) 16 bits. on 16- and 8-bit systems long provided range at a cost of efficiency.

cheers & hth.,

Upvotes: 5

Nate
Nate

Reputation: 30666

From one of the answers in the question you linked:

The long must be at least the same size as an int, and possibly, but not necessarily, longer.

I can't think of a better way to explain it.

Upvotes: 6

Related Questions