C Programming Basics
Which of the following is what the C standard specifies about the size of the int type?
- AIt is fixed at 32 bits in every implementation
- BOnly that it is at least 16 bits is specified; the actual size is decided by the implementation
- CIt is specified to equal the width of the CPU's addresses, so it is 64 bits on a 64-bit CPU
Show answer and explanation
Answer: B. Only that it is at least 16 bits is specified; the actual size is decided by the implementation
The C standard only requires that int can represent at least −32767 to 32767 (that is, 16 bits or more); the actual size is decided by the implementation (the combination of compiler and target CPU). With compilers for 16-bit microcontrollers int may be 16 bits, while on most PCs and 32-bit microcontrollers it is 32 bits. Even on 64-bit Linux and Windows, int generally stays 32 bits and pointers become 64 bits. When an integer of a fixed size is needed, int32_t and similar types from stdint.h are used.
- A. 32 bits is the size in many environments, but it is not a rule of the standard. Some implementations use a 16-bit int.
- B. Correct. The standard only sets a lower limit on the range, and the implementation decides the actual size.
- C. Even on 64-bit Linux and Windows, int is generally 32 bits.