Yarel
Yarel

Reputation: 444

C word size and standard size

in this article, taken from the book "Linux kernel development": http://www.makelinux.net/books/lkd2/ch19lev1sec2
it says:

The size of the C long type is guaranteed to be the machine's word size. On the downside, however, code cannot assume that the standard C types have any specific size. Furthermore, there is no guarantee that an int is the same size as a long

Question is, i thought int is the same as the word size, not long, and i couldn't find any official standard which defines this saying.

any thoughts?

Upvotes: 5

Views: 7218

Answers (3)

ouah
ouah

Reputation: 145899

The size of the C long type is guaranteed to be the machine's word size.

This is wrong for a lot of platforms. For example, in the embedded world usually 8-bit MCU (e.g., HC08) have a 8-bit word size and 16-bit MCU (e.g., MSP430) have a 16-bit word size but long is 32-bit in these platforms. In Windows x64 (MSVC compiler), the size of a word is 64-bit but long is 32-bit.

Upvotes: 4

The C standard does not know what a word is, and a C implementation might do things in unusual ways. So your book is wrong. (for example, some C implementation might use 64 bits long on a 8 bit micro-controller).

However, the C99 standard defines the <stdint.h> header with types like intptr_t (an integral type with the same size as void* pointers) or int64_t (a 64 bits integer) etc.

See also this question, and wikipedia's page on C data types.

Upvotes: 2

Potatoswatter
Potatoswatter

Reputation: 137920

Sometimes, people on the Internet are wrong. The sizes are fixed by the ABI. Linux ports don't necessarily create an original ABI (usually another platform or manufacturer recommendation is followed), so there's nobody making guarantees about int and long. The term "machine word" is also very ill-defined.

Upvotes: 7

Related Questions