TRT
Sign in
easyFoundationsconcept

When should you use fixed-width integers like uint32_t and size_t instead of int, and how can mixing signed and unsigned types break comparisons?

Explain platform data models (LP64 vs LLP64), why wire protocols and binary headers require <stdint.h> types, and how usual arithmetic conversions turn -1 > 1U true.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read
Learn it firstFixed-Width Types and ConversionsEliminate cross-platform data model ambiguities using stdint.h, format fixed types with inttypes.h, and master casting semantics.

A strong answer

The C standard only guarantees minimum bit widths for fundamental types (int is at least 16 bits, long at least 32 bits). Across 64-bit operating systems, C uses different data models: 64-bit Linux and macOS use LP64 (int is 32-bit, long and pointers are 64-bit), whereas 64-bit Windows uses LLP64 (int and long are both 32-bit, and only long long and pointers are 64-bit). Whenever code serializes structs to disk, parses network headers, or computes cryptographic hashes, relying on long or int breaks portability. Including <stdint.h> (uint8_t, int32_t, uint64_t) guarantees exact bit widths everywhere, while size_t and uintptr_t are guaranteed to match the target platform's object size and pointer width.

A classic bug occurs when mixing signed and unsigned types of the same rank—such as comparing int x = -1; with unsigned int y = 1U; (or size_t). Under C's usual arithmetic conversions, when a signed integer is compared with an unsigned integer of equal or greater rank, the signed value is converted to unsigned by adding $2^N$. For 32-bit integers, -1 converts to UINT32_MAX (4,294,967,295), so the expression -1 < 1U evaluates to false (4294967295 < 1 is 0)!

What the interviewer is listening for

  • Knows the difference between LP64 (Unix) and LLP64 (Windows) and why <stdint.h> is mandatory for binary formats and hardware registers.
  • Explains C's integer promotion (types narrower than int promote to int) and usual arithmetic conversion rules (signed converts to unsigned when ranks match or unsigned is wider).
  • Uses size_t for byte counts and array lengths, while avoiding unsigned underflow in reverse loops.

Common mistakes

  • Writing for (size_t i = n - 1; i >= 0; i--), which never terminates because size_t is unsigned (0 - 1 wraps around to SIZE_MAX, which is still >= 0).
  • Assuming sizeof(long) == sizeof(void *) on all 64-bit platforms (false on 64-bit Windows).
  • Ignoring -Wsign-compare warnings when comparing a signed error-capable variable (ssize_t or int) against an unsigned size_t threshold.

This answer walks

Practice follow-ups

  1. 01What happens when a countdown loop is written as for (size_t i = n - 1; i >= 0; i--)?
  2. 02Why is int 32 bits on both 64-bit Linux and 64-bit Windows while long is 64 bits on Linux but 32 bits on Windows?

Not started

Sign in to save your learning progress.

Sign in to save