basics · beginner · ~10 min

Print sizeof for the canonical C types

Memorize the type sizes on the platform you build on.

Challenge

Implement void print_sizes(int *out) that fills out[] with the sizeof of char, short, int, long, size_t, void* in that order. Six entries.

Why this matters

Pen testers and reverse engineers must know type sizes at sight. A 4-byte signed integer overflow at INT_MAX is the start of half the integer-overflow CVEs in C.

Input format

out of at least 6 ints.

Output format

out[0..5] populated.

Constraints

No printf inside; just write to out.

Starter code

void print_sizes(int *out) { /* TODO */ }

Common mistakes

Assuming int is 4 bytes everywhere — it usually is on 32/64-bit Linux/macOS but not on every embedded platform.

Edge cases to handle

LP64 (Linux/macOS): long is 8 bytes; LLP64 (Windows): long is 4 bytes.

Complexity

O(1).

Background lessons

Up next

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.