4

On my system, I get:

sizeof ( int )  = 4
sizeof ( long ) = 4

When I checked with a C program, both int & long overflowed to the negative after:

a = 2147483647;
a++;

If both can represent the same range of numbers, why would I ever use the long keyword?

8 Answers8

7

int has a minimum range of -32767 to 32767, whereas long has a minimum range of -2147483647 to 2147483647.

If you are writing portable code that may have to compile on different C implementations, then you should use long if you need that range. If you're only writing non-portable code for one specific implementation, then you're right - it doesn't matter.

caf
  • 233,326
  • 40
  • 323
  • 462
  • 4
    Use types from `stdint.h` when size matters. – Paul R Sep 29 '10 at 10:13
  • 2
    In most cases, what one needs is a minimum guaranteed range, in which case the standard types are ample. In the cases when your requirements are narrower, the types from `` can be useful. – caf Sep 29 '10 at 13:32
  • It should be noted that POSIX requires `int` to be at least 32-bit, and of course Windows has 32-bit `int`. Code that works on Windows and on any POSIX system certainly has an above-average level of portability. :-) – R.. GitHub STOP HELPING ICE Sep 30 '10 at 03:18
6

Because sizeof(int)==sizeof(long) isn't always true. int normaly represents the fastest size with at least 2*8 Bit. long on the other hand is at least 4*8 Bit.

Juri Robl
  • 5,614
  • 2
  • 29
  • 46
  • they are different on 64bit modes for example. int is 32bit for both modes (on Solaris at least) but it varies between platforms as you say. – Chris Huang-Leaver Sep 29 '10 at 10:08
5

C defines a number of integer types and specifies the relation of their sizes. Basically, what it says is that sizeof(long long) >= sizeof(long) >= sizeof(int) >= sizeof(short) >= sizeof(char), and that sizeof(char) == 1.

But the actual sizes are not defined, and depend on the architecture you are running on. On a 32-bit PC, int and long are typically four bytes and long long is 8 bytes. But on a 64-bit system, long is typically 8 bytes, and thus different from int.

There is also a type called uintptr_t (and intptr_t) that is guaranteed to have the same size as data pointers.

The important thing to remember is to not assume that you can, for example, store pointer values in a long or an int. Being portable is probably more important than you think, and it is likely that you will want to compile your code on a 64-bit system in the near future.

dkagedal
  • 578
  • 2
  • 7
  • 14
  • I can't find a paragraph in the C89 Standard, where it says that sizeof(long) >= sizeof(int). So I think it would be allowed that sizeof(long) < sizeof(int)? – Juri Robl Sep 29 '10 at 10:31
  • @Juri from 2.2.4.2 Numerical limits: Their implementation-defined values shall be equal _or greater_ in magnitude (absolute value) to those shown, with the same sign. – Christoffer Sep 29 '10 at 10:36
  • 1
    @Juri Robl: In the C89 Standard, §3.1.2.5 says *"In the list of signed integer types above, the range of values of each type is a subrange of the values of the next type in the list."*. In the C99 Standard it is §6.2.5p8 (the range of a type with lesser integer conversion rank must be a subrange of types with greater integer conversion rank) together with §6.3.1.1p1 (`long int` has greater integer conversion rank than `int`). – caf Sep 29 '10 at 11:00
0

I think it's more of a compiler issue nowadays, since computers has gone much faster and demands more numbers, as was the case before.

arscariosus
  • 1,326
  • 2
  • 13
  • 19
0

On different platform or with a different compiler, the int and long may be different. If you don't plan to port your code to anything else or use a different machine, then pick the one you want, it won't make a difference.

Matthieu
  • 16,103
  • 10
  • 59
  • 86
0

It depends on the compiler, and you might want to check this out: What does the C++ standard state the size of int, long type to be?

Community
  • 1
  • 1
Ruel
  • 15,438
  • 7
  • 38
  • 49
0

The size of built-in data types is variable depending on the C implementation, but they all have minimum ranges. Nowadays, int is typically 4 bytes long (32-bits) because most OS are 32-bit. Note that char will always be 1 bytes.

Alexander Rafferty
  • 6,134
  • 4
  • 33
  • 55
  • 1
    sizeof(char) will always be 1, but CHAR_BITS doesn't have to be 8. It's perfectly legal to have 32-bits chars, and if ints are 32-bits as well, sizeof(int) will be 1. – Sjoerd Sep 29 '10 at 10:19
  • I've never heard of a non 8-bit byte. – Alexander Rafferty Sep 29 '10 at 10:34
  • POSIX requires `CHAR_BIT==8`, and of course Windows has `CHAR_BIT==8`, so for all intents and purposes, `CHAR_BIT` is 8. The notable exceptions are ancient legacy mainframes (largely irrelevant, where `CHAR_BIT` is often 9 or 10) and DSPs (where `CHAR_BIT` might be 32 and `sizeof(char)`, `sizeof(short)`, `sizeof(int)`, and `sizeof(long)` all 1. – R.. GitHub STOP HELPING ICE Sep 30 '10 at 03:21
  • There are also some DSP's where CHAR_BIT isn't 8; I've coded for one where CHAR_BIT is 16, and sizeof(int)==sizeof(short)==sizeof(char)==1. Note that the requirement that a 'char' (of unspecified signedness) fit in an 'int' implies that 'char' on such a system must be signed. – supercat Feb 14 '11 at 21:43
0

The size of a data type depends upon the compiler. Different compilers have diffrent size of int and other data types.

So if you make a code which is going to run on diffrent machine you should use long or it is depend on the range of the value tha t ur variable may have.

Sambhav jain
  • 882
  • 2
  • 12
  • 16