17

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?

Community
  • 1
  • 1
Alex
  • 241
  • 2
  • 3
  • 4

7 Answers7

28

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.

slebetman
  • 109,858
  • 19
  • 140
  • 171
  • @Anon: Can you cite where in the standard it says so? I have seen compilers in real life where char is 32 and 64 bits. Or maybe that's just C and not C++. – slebetman Dec 01 '10 at 21:33
  • @Anon: that's an incorrect correction. the size of `char` in bits is given by e.g. `CHAR_BIT` from ``. it must >= 8. – Cheers and hth. - Alf Dec 01 '10 at 21:34
  • 5
    Almost, `sizeof(char) == 1` but `CHAR_BIT` can be an arbitrary number. The only requirement is that will hold the entire basic charset for the platform. – Šimon Tóth Dec 01 '10 at 21:34
  • 1
    @pst: I believe `long` is a modifier to `int` that implies `int` so that the `int` keyword is optional. Some people prefer it there to clarify that we are dealing with integers, not floats. – slebetman Dec 01 '10 at 21:36
  • @Let_Me_Be: that's an incorrect correction. `CHAR_BIT` must be >= 8. In order to be able to represent the required number range (from the C standard). – Cheers and hth. - Alf Dec 01 '10 at 21:36
  • @Anon: No worries, I had the same reaction the first time someone told me that char can be 32 bits. It's like everything I thought I knew was a lie or something :-P – slebetman Dec 01 '10 at 22:02
21

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
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
6

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.

Nate
  • 30,286
  • 23
  • 113
  • 184
5

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.,

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • 1
    Really? Where are these guarantees? – Tabber33 Dec 01 '10 at 21:33
  • 1
    @Tabber33: in the C standard, which the C++ standard refers to. Hth., – Cheers and hth. - Alf Dec 01 '10 at 21:38
  • 1
    The C++ standard refers to the C standard? Which C standard? This is news to me... I've read the C++03 standard, and I was never referred to the C standard (which I don't have). – Tabber33 Dec 01 '10 at 21:39
  • 1
    @Tabber33: the current C++ standard is C++98, with the error corrections of Technical Corrigendum 1, known as C++03. Since 1998 comes before 1999, you can easily deduce that the current C++ standard does not refer to C99. Hence it refers to? – Cheers and hth. - Alf Dec 01 '10 at 21:40
  • @Tabber33: I didn't notice that you state that you have read the C++03 standard. You have not. – Cheers and hth. - Alf Dec 01 '10 at 21:42
  • 1
    Please post the relevant section of the C++03 standard where `long` is _guaranteed_ to be at least 32 bits. – Tabber33 Dec 01 '10 at 21:42
  • @Tabber: You're a troll. You have been referred to the C standard. – Cheers and hth. - Alf Dec 01 '10 at 21:44
  • Yup, a troll who challenges people who post completely innacurate answers and resort to personal attacks when asked to provide evidence. You sir are a model SO citizen. Cheers. – Tabber33 Dec 01 '10 at 21:52
  • @Tabber33: you're now lying about me personally, in addition to earlier technical disinformation about the standards. – Cheers and hth. - Alf Dec 01 '10 at 21:57
  • @Tabber33: it's pretty silly of you to downvote this answer without downvoting the other answer that says the same thing. are you really 33 years old? i doubt it. – Cheers and hth. - Alf Dec 01 '10 at 22:10
  • I DID NOT DOWNVOTE YOUR ANSWER! I don't even have enough rep to downvote, but believe me, I would if I could. Still no evidence of your claims? That's ok, just keep trashing people who don't accept your statements as the gospel. Thanks for making my choice to never interact with you again very obvious! – Tabber33 Dec 01 '10 at 22:14
  • @Tabber33: i'm sorry for thinking it was you. you have presented overwhelming evidence that it couldn't have been you. after all, as you say, the nick Tabber33 doesn't have the rep, and and one can only have one nick on SO, and besides, you'd never do any such thing. – Cheers and hth. - Alf Dec 01 '10 at 22:21
  • 4
    @Tabber33: C++03 §3.9, footnote 37 says, "The intent is that the memory model of C++ is compatible with that of ISO/IEC 9899 Programming Language C." This implies that all statements regarding POD types have a basis in C89. It also refers to `` in footnote 39 in §3.9.1 for this definition. I don't have a copy of C89, but C99 §5.2.4.2.1 says `long` must be able to hold at least a 32-bit integer. Therefore, `long` is guaranteed to be *at least* 32-bit. QED. – greyfade Dec 01 '10 at 22:22
  • Wow! I hope you're in a padded room. Good luck with all that. – Tabber33 Dec 01 '10 at 22:26
  • @greyfade: I'm not sure that "memory model compatibility" implies any datatype size requirements though... that is your assumption. We'd have to understand what "memory model compatibility" really means. But, thanks for citing the standard nonetheless. – Tabber33 Dec 01 '10 at 22:32
  • 1
    @Tabber33: What's to understand? To be compatible with C89's memory model, POD types must have identical size, padding, and alignment. Read the whole section on alignment. – greyfade Dec 01 '10 at 22:34
  • @greyfade: thanks for that, but I'm not sure a footnote is normative. however, given that the normative text includes most of the C standard library, and given the intent of being able to use libraries compiled as C (which was much of the point of C++, and prime reason for its success), it would require a so called "perverse implementation" to not fulfill the C requirements. actually, a sufficently "perverse" implementation can accept COBOL code and be formally compliant, as long as it formally emits a diagnostic (which can be defined as anything). so, the formal game is not always useful... – Cheers and hth. - Alf Dec 01 '10 at 22:39
  • @Alf: The whole point of that footnote is to reinforce that the *whole intent* of the section is compatibility with C. I can't imagine how that could possibly not qualify as "normative." – greyfade Dec 01 '10 at 22:41
  • @greyfade: yes, i agree in spirit! in the standards world however, some words have very special meaning. i know for certain that *comments* (including example comments) are not "normative". for example, C++98 §5/4 contains incorrect examples, but happily the examples are non-normative. however, i'm not sure about footnotes. anyway, just goes to show that the formal game isn't always practically useful, and provides shaky ground sometimes. but again, thanks! ;-) – Cheers and hth. - Alf Dec 01 '10 at 22:49
  • @Taber33: Alf is correct. A `long` is guaranteed by the standard to be at least 32 bits. Note that there is no guarantee about how many bytes that is. See here for reference: http://stackoverflow.com/questions/4329777/is-long-guaranteed-to-be-at-least-32-bits-not-a-newbie-question\ – John Dibling Dec 02 '10 at 00:15
  • @anonymous downvoter: please explain your downvote, so that other can ignore it. – Cheers and hth. - Alf Oct 05 '12 at 18:04
4

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.
icecrime
  • 74,451
  • 13
  • 99
  • 111
0

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).

Community
  • 1
  • 1
pythonic metaphor
  • 10,296
  • 18
  • 68
  • 110
  • Use of types like `int64_t` is unfortunately not sufficient to ensure portable code. Given `uint16_t ffff16=0xFFFF; int64_t whoknows=ffff16*ffff16;` is required to yield 1 on machines where `int` is 16 bits and 4294836225 on machines where it's 33 bits or greater. On machines where `int` is an intermediate size, it will typically yield -131071, but from my understanding compilers for 32-bit machines could do anything they wanted since the multiplication would overflow. – supercat Nov 04 '14 at 00:10
0

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.

Community
  • 1
  • 1
dan04
  • 87,747
  • 23
  • 163
  • 198