tags:

views:

112

answers:

8
+4  Q: 

int v/s. long in C

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?

+7  A: 

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
Use types from `stdint.h` when size matters.
Paul R
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 `<stdint.h>` can be useful.
caf
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..
A: 

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
A: 

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
A: 

It depends on the compiler, and you might want to check this out: http://stackoverflow.com/questions/589575/c-size-of-int-long-etc

Ruel
+6  A: 

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
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
A: 

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
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
I've never heard of a non 8-bit byte.
Alexander Rafferty
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..
+5  A: 

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
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
@Juri from 2.2.4.2 Numerical limits: Theirimplementation-defined values shall be equal _or greater_ in magnitude(absolute value) to those shown, with the same sign.
Christoffer
@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
Thanks, I didn't know that.
Juri Robl
A: 

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