4

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?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Yarel
  • 424
  • 1
  • 6
  • 15
  • 1
    http://stackoverflow.com/questions/589575/size-of-int-long-etc – Rohit Vipin Mathews Apr 03 '12 at 07:15
  • 1
    Many questions are related to this one, for instance: [Any guaranteed minimum sizes for types in C?](http://stackoverflow.com/q/1738568/45249) Its most voted answer redirects to Wikipedia: [C data types](http://en.wikipedia.org/wiki/C_data_types). – mouviciel Apr 03 '12 at 07:16

3 Answers3

7

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.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
4

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.

ouah
  • 142,963
  • 15
  • 272
  • 331
2

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.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547