basics · beginner · ~10 min

Print sizeof for the canonical C types

Memorize the type sizes on the platform you build on.

Challenge

Record the size in bytes of the fundamental C types on the platform you compile for.

Task

Implement void print_sizes(int *out) that fills out[0..5] with sizeof applied to char, short, int, long, size_t, and void*, in exactly that order. Write nothing else; do not print.

Input

A pointer out to an array of at least 6 ints.

Output

No return value. After the call: out[0]=sizeof(char), out[1]=sizeof(short), out[2]=sizeof(int), out[3]=sizeof(long), out[4]=sizeof(size_t), out[5]=sizeof(void*).

Example

print_sizes(out);   ->   out == {1, 2, 4, 8, 8, 8}   (typical 64-bit Linux/macOS)

Edge cases

  • Sizes are platform-dependent: long is 8 bytes on LP64 (Linux/macOS) but 4 bytes on LLP64 (Windows). The grader checks against the build platform's own sizeof, so report the real values.

Rules

  • Do not call printf — just store the sizes into out.

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

A pointer out to at least 6 ints.

Output format

No return value; out[0..5] set to sizeof of char, short, int, long, size_t, void* in order.

Constraints

No printf; just write to out. Cast each sizeof (a size_t) to int.

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.