5.1.2 · D3C Programming

Worked examples — Data types — char, short, int, long, float, double, size_t

2,571 words12 min readBack to topic

The scenario matrix

Before working examples, let us enumerate the case classes. Think of this like listing every room in a house before we clean each one — we want to guarantee no room is skipped.

Cell Case class What makes it tricky Example
A Signed value in range baseline, no wrap Ex 1
B Signed value above max (positive overflow) wraps to negative Ex 2
C Signed value below min (negative overflow) wraps to positive Ex 3
D Unsigned underflow (0 − 1) wraps to the huge top value Ex 4
E Zero / degenerate input boundary, easy to mis-handle Ex 5
F Limiting value (exactly MAX) the edge itself, +1 tips over Ex 6
G Float rounding (no finite binary form) == fails Ex 7
H Signed ↔ unsigned mix silent comparison flip Ex 8
I Real-world word problem pick the right type Ex 9
J Exam twist (sizeof + promotion) trap combining two rules Ex 10

We use the term wrap to mean: when a value goes past the end of a type's range, it re-enters from the other end — like an odometer rolling from 999 back to 000. The figure below is the mental model for every integer example on this page.

Figure — Data types — char, short, int, long, float, double, size_t

Cell A — Signed value in range (baseline)


Cell B — Positive overflow (above max)


Cell C — Negative overflow (below min)


Cell D — Unsigned underflow (0 − 1)


Cell E — Zero / degenerate input


Cell F — Limiting value (exactly MAX, then +1)


Cell G — Float rounding (no finite binary form)

Figure — Data types — char, short, int, long, float, double, size_t

Cell H — Signed ↔ unsigned mix (silent comparison flip)


Cell I — Real-world word problem (pick the type)


Cell J — Exam twist (sizeof + promotion trap)


Active Recall

Recall Cover the answers — one per matrix cell
  • unsigned char u = 250; u += 10; → ? ::: 4 (260 mod 256)
  • signed char c = -100; c -= 50; → ? ::: 106 (−150 + 256), formally UB
  • unsigned x = 0; x -= 1; → ? ::: 4294967295 ()
  • UINT_MAX + 1 → ? ::: 0 ( mod )
  • 0.1 + 0.2 == 0.3 → ? ::: 0 (false, binary rounding)
  • int -1 < unsigned 1 → ? ::: 0 (false; −1 becomes huge)
  • Type for a 5 GB size → ? ::: size_t (holds 5368709120)
  • sizeof('A') in C → ? ::: 4 ('A' is an int)