An int stores signed integer data, just like a short int, but it can use more bytes. This note covers int, unsigned int, long, and unsigned long, and why the size of these types is decided by the compiler. It continues from Short Int Data Type, where the size was fixed at 2 bytes on every compiler.

Int Data Type

A variable of type int is used to store signed integer data, just like a short int. The difference is the size: an int is greater than a short, or in the best case equal to it.

An int variable consumes 2 bytes or 4 bytes of memory. Which one you get depends on the compiler, because the size of an int is decided by the compiler being used to generate code for your target hardware. It is typically 2 or 4 bytes.

You can check the size on your own machine with sizeof(int), as shown in sizeof Operator:

printf("%zu\n", sizeof(int));   // prints 2 or 4 on typical compilers

Unsigned Int Data Type

A variable of type unsigned int is used to store unsigned integer data. It follows the same rule as unsigned short from Short Int Data Type > Deriving the Range: there is no sign bit, so every bit stores magnitude and no negative values can be held. Because an int may be 2 or 4 bytes, its range also depends on the compiler.

Long and Unsigned Long Data Types

A variable of type long is used to store signed integer data, like int and short int. A long variable consumes 4 bytes or 8 bytes of memory. That is the important difference from int.

Again, the size of a long is decided by the compiler being used to generate code for your target hardware, so it is typically 4 or 8 bytes.

A variable of type unsigned long is used to store unsigned integer data, so like the other unsigned types it has no sign bit and holds only zero and positive values.

Why the Compiler Decides the Size

The compiler generates machine code for a specific piece of hardware, and the hardware decides how big a number it can handle in one go. A small processor handles numbers best 2 bytes at a time, so its compilers make int 2 bytes. A desktop processor handles 4 bytes at a time, so its compilers make int 4 bytes. This is why the C rule for sizes is an ordering, not a fixed number:

Every symbol in this formula, one by one:

  • short, int, long: the three integer data types of C.
  • size of: how many bytes of memory a variable of that type consumes.
  • $\le$: is less than or equal to, so each type is allowed to be the same size as, or bigger than, the one before it.

Read plainly, the formula says: as you go from short to int to long, the size can only grow or stay the same, never shrink. This matches what the lectures showed: short is always 2 bytes, int is 2 or 4, and long is 4 or 8.

Worked check with real numbers: on a typical desktop compiler short is 2 bytes, int is 4 bytes, and long is 8 bytes. Then holds, so the rule is satisfied. You can see this in the real compiler tables of Data Types And Variables: GCC on a desktop gives long 8 bytes, while the ARMCC compiler gives the same long only 4 bytes, because its target hardware handles 32-bit words.

Sizes at a Glance

TypeWhat it storesTypical sizeDecided by
short intsigned data2 bytesalways 2, on every compiler
unsigned short intunsigned data2 bytesalways 2, on every compiler
intsigned data2 or 4 bytesthe compiler for your target hardware
unsigned intunsigned data2 or 4 bytesthe compiler for your target hardware
longsigned data4 or 8 bytesthe compiler for your target hardware
unsigned longunsigned data4 or 8 bytesthe compiler for your target hardware

short is the odd one out

A short is always 2 bytes, so you can rely on its range everywhere. For int and long there is no fixed size, so use sizeof Operator to check what your compiler gives you.

Previous: Short Int Data Type