tags:

views:

188

answers:

8

Why is it that I'm finding the size of int and long int are shown to be 4 bytes? Is it that the int is by default long int?

+3  A: 

They just happen to have the same size.

This post summarizes what you can expect out of data types with respect to size and range.

GMan
+7  A: 

The size of int is neither guaranteed to be 4 nor to be equal to the size of long int. Put in other words: that's completely implementation defined.

sepp2k
+2  A: 

No, int and long are not necessarily the same size, even though that happens to be the case in your compiler.

The C standard defines a minimum size for each of these datatypes, but it is up to the implementation what the actual size is. For example, some systems have 2-byte ints and 4-byte longs, while others may have 4-byte ints and 8-byte longs.

Michael Madsen
A: 

The only guarantee made by the standard is that sizeof(long) >= sizeof(int).

In the old days of 16-bit processors, it wasn't uncommon for int to be 2 bytes.

Mark Ransom
+9  A: 
sizeof(short) <= sizeof(int) <= sizeof(long)

That's all you can count on. The rest is completely up to the implementation. In the olden days of DOS, 16 bit compilers usually has sizeof(int) == sizeof(short) == 2. On 32 bit systems, sizeof(int) is usually equal to sizeof(long) == 4. As a rule of thumb, int is the type the processor can work with the fastest. No rule without exceptions...

EDIT: Removed the second rule, sizeof(short) < sizeof(long), which is NOT part of the C standard. On some platforms, sizeof(short) may actually be equal to sizeof(long).

lbruder
Good answer, +1!
Santiago Lezica
@Martin: It's not a logical progression, they're two separate rules. IIRC, the standard says that short must be smaller than long.
Javier Badia
I don't think that short is < than long.
Johannes Schaub - litb
@Martin: It's alright, I thought it did because of how the question was stated.
Javier Badia
@lbruder: Old comments gone. See http://stackoverflow.com/questions/271076/what-is-the-difference-between-an-int-and-a-long-in-c/271132#271132 for full details.
Martin York
+1  A: 

The only guarantees the Standard mandates are (assume all expressions below are sizeof (type) instead of just type)

char <= short <= int <= long <= long long

so you can have

char == short == int == long == long long /* Cray?? */
char <  short <  int == long <  long long /* Windows 32 bit */
char <  short <  int <  long == long long /* Linux 64 bit */
pmg
It's widely rumored, but at least in general false, that they're all equal on Cray's. There may have been some really early compiler for which it was true (I can't claim to have seen *every* one) but certainly most had 8-bit char's.
Jerry Coffin
Many years ago I worked on a custom processor design project where the C compiler had `sizeof(char) == sizeof(short) == sizeof(int) == sizeof(long) == 1`. IIRC, `float` and `double` were also the same size. `char` is by convention the smallest addressable increment. In this machine, that was a 32-bit word. This was well before C89, incidentally.
RBerteig
There are still DSP's with 32-bit chars.
ninjalj
A: 

The C standard makes the following guarantees about the conversion ranks of the standard integer types and their precisions:

_Bool (1) <  char (8)
          <= short (16)
          <= int (16)
          <= long (32)
          <= long long (64)

In case of _Bool, the precision is exact, whereas the others are minimum values. There may also be additional, so-called extended integer types.

A char is the smallest addressable unit of memory, so sizeof (char) == 1, regardless of its precision (which is given by CHAR_BIT). The sizes of the other standard integer types are imlementation-defined. I suspect sizeof (_Bool) will also be 1, but I couldn't find anything in the standard which actually guarantees this...

Christoph
A: 

On modern 32-bit operating systems, int and long are generally 32bit, or ILP32 (integer, long, pointer), while on 64-bit operating systems, LP64 is common, implying that int is 32 bits. long long is 64 bits just about everywhere, which may or may not be the same as long.

There are usually macros you can test for, like ILP32, ILP32, etc. that will be set if your environment uses that set of types.

Chris