2

Does the ANSI C specification call for size of int to be equal to the word size (32 bit / 64 bit) of the system?

In other words, can I decipher the word size of the system based on the space allocated to an int?

4 Answers4

6

The size of the int type is implementation-dependent, but cannot be shorter than 16 bits. See the Minimum Type Limits section here.

This Linux kernel development site claims that the size of the long type is guaranteed to be the machine's word size, but that statement is likely to be false: I couldn't find any confirmation of that in the standard, and long is only 32 bits wide on Win64 systems (since these systems use the LLP64 data model).

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
  • I don't see any explicit discussion of word sizes either. Note that 64-bit editions of Windows have both `int` and `long` 32-bit. As far as I know, that decision is compliant with the C standard. – Matthew Flaschen Nov 21 '10 at 06:41
  • @Matthew, absolutely, I updated my answer with that information. – Frédéric Hamidi Nov 21 '10 at 06:46
  • édéric, I didn't mean Windows overall was compliant with the C standard, just the type size part. :) There are many other parts of C99 they ignore. – Matthew Flaschen Nov 21 '10 at 06:51
3

The language specification recommends that int should have the natural "word" size for the hardware platform. However, it is not strictly required. If you noticed, to simplify 32-bit-to-64-bit code transition some modern implementations prefer to keep int as 32-bit type even if the underlying hardware platform has 64-bit word size.

And as Frederic already noted, in any case the size may not be smaller than 16 value-forming bits.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
1

The original intension was the int would be the word size - the most efficient data-processing size. Still, what tends to happen is that massive amounts of code are written that assume the size of int is X bits, and when the hardware that code runs on moves to larger word size, the carelessly-written code would break. Compiler vendors have to keep their customers happy, so they say "ok, we'll leave int sized as before, but we'll make long bigger now". Or, "ahhh... too many people complained about us making long bigger, we'll create a long long type while leaving sizeof(int) == sizeof(long)". So, these days, it's all a mess:

Does the ANSI C specification call for size of int to be equal to the word size (32 bit / 64 bit) of the system?

Pretty much the idea, but it doesn't insist on it.

In other words, can I decipher the word size of the system based on the space allocated to an int?

Not in practice.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
0

You should check your system provided limits.h header file. INT_MAX declaration should help you back-calculate what is the minimum size an integer must have. For details look into http://www.opengroup.org/onlinepubs/009695399/basedefs/limits.h.html

Fanatic23
  • 3,378
  • 2
  • 28
  • 51
  • 1
    That is one way to work out what sizeof(int) is, but doesn't provide a cross-platform comment on the questions (Does the ANSI C specification call for size of int to be equal to the word size (32 bit / 64 bit) of the system? Can I decipher the word size of the system based on the space allocated to an int?) – Tony Delroy Nov 22 '10 at 02:15