C data model portability: does the size (number of bytes) of short int and long int vary across platforms and ABIs?

Difficulty: Easy

Correct Answer: Yes — sizes vary by platform and ABI

Explanation:


Introduction / Context:
This question checks knowledge of the C data model and portability. Unlike types such as uint32_t from stdint.h, the fundamental integer types short and long have implementation-defined sizes subject to minimum ranges in the standard.



Given Data / Assumptions:

  • The C standard guarantees relative ordering: sizeof(char) ≤ sizeof(short) ≤ sizeof(int) ≤ sizeof(long) ≤ sizeof(long long).
  • Minimum widths are specified, but exact byte sizes depend on the platform and ABI (e.g., ILP32, LP64, LLP64).
  • Examples: many Unix-like 64-bit systems use LP64 (long = 8 bytes), while Windows 64-bit uses LLP64 (long = 4 bytes).


Concept / Approach:
The standard defines ranges, not fixed sizes. A short is at least 16 bits; a long is at least 32 bits and no smaller than an int. Therefore, both short and long can vary across compilers and architectures.



Step-by-Step Solution:
Identify data model: ILP32 (int, long, pointer are 32-bit), LP64 (long, pointer 64-bit), LLP64 (long long and pointer 64-bit; long 32-bit).Map consequences: short often 16 bits; long may be 32 or 64 bits.Conclude variability: neither short nor long has a universally fixed byte size.



Verification / Alternative check:
Printing sizeof(short) and sizeof(long) on different targets confirms differing values (e.g., 2/8 on LP64 vs 2/4 on LLP64).



Why Other Options Are Wrong:
Fixed by the standard: false; only minima and relations are fixed.Only long or only short varies: both can vary.Variation only on embedded: variation exists on desktops and servers too.



Common Pitfalls:
Coding with hard-coded sizes; failing to use stdint.h types when exact widths are required.



Final Answer:
Yes — sizes vary by platform and ABI

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion