user191776
user191776

Reputation:

Size of an Integer in C

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?

Upvotes: 2

Views: 5139

Answers (4)

Frédéric Hamidi
Frédéric Hamidi

Reputation: 262979

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).

Upvotes: 6

Tony Delroy
Tony Delroy

Reputation: 106116

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.

Upvotes: 1

Fanatic23
Fanatic23

Reputation: 3428

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

Upvotes: 0

AnT stands with Russia
AnT stands with Russia

Reputation: 320531

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.

Upvotes: 3

Related Questions