0

I know that nowadays virtually all compilers use 4 bytes for an int, but if I'm not mistaken it wasn't always that way.

I'm also aware that on Intel architectures the size of an int always matched the CPU. For example on 8-bit CPUs an int was 8 bits.

My question is what, if anything, does the C standard have to say about the size of an int?

  • 1
    There are several C standards, and several ways of understanding them. Read first [n1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) – Basile Starynkevitch Aug 07 '21 at 17:56
  • Try to compile `printf("%u",sizeof(int));` on a 16 bit system and then on a 32 and a 64 bit systems ... :) – Sir Jo Black Aug 07 '21 at 18:04
  • 2
    "_virtually all compilers use 4 bytes for an int_" - not true. For 64-bit Linux targets it is 8. "_For example on 8-bit CPUs an int was 8 bits._" also untrue - it is _required_ to be _at least_ 16. Having `char`, `short` and `int` all the same size would make little sense. This question has little merit since you could just Google it. https://en.wikipedia.org/wiki/C_data_types. The standard defines them in terms of _minimum_ width/range and relative sizes (e.g. an `int` must be as wide or wider than a `short`). – Clifford Aug 07 '21 at 18:08
  • @Clifford thanks! I did Google it, but I didn't find that – Abraham Murciano Benzadon Aug 07 '21 at 18:10
  • What did you use for a search term? _"c data type sizes"_ seems most obvious. Need _Google Fu_. – Clifford Aug 07 '21 at 18:13
  • I should of course have been flamed by now for claiming that `int` was 64 bit in Linux 64 - it is not - it is `long` that is 64 bit. Apologies. If it were 64 bit, you'd have to have a gap in the 8, 16, 32, 64 bit size progression, so effectively 16 is the minimum, but 32 the practical maximum to allow a type for each of 8, 16 and 32. – Clifford Aug 07 '21 at 18:24

2 Answers2

5

My question is what, if anything, does the C standard have to say about the size of an int?

A ‘‘plain’’ int object has the natural size suggested by the architecture of the execution environment (large enough to contain any value in the range INT_MIN to INT_MAX as defined in the header <limits.h>).

and

Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign.

— minimum value for an object of type int INT_MIN -32767 // −(2^15 − 1)

— maximum value for an object of type int INT_MAX +32767 // 2^15 − 1

so you can't have an 8-bit int on a compliant implementation.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
3

According to the C standard ISO/IEC 9899 (latest is 2018) the size of an integer is implementation defined.

Likewise, the size of char is likewise implementation define - not necessarily 8 bits!

Andrew
  • 2,046
  • 1
  • 24
  • 37