5.1.3C Programming

Type sizes — sizeof, platform dependency, stdint.h (int32_t etc.)

1,886 words9 min readdifficulty · medium

WHY does C leave sizes unspecified?


WHAT the standard actually guarantees


Common ABIs (the platform dependency)

Figure — Type sizes — sizeof, platform dependency, stdint.h (int32_t etc.)
Type LP64 (Linux/macOS 64-bit) LLP64 (Windows 64-bit) 16-bit (old DOS)
char 1 1 1
short 2 2 2
int 4 4 2
long 8 4 4
long long 8 8 8
pointer 8 8 2/4

HOW to measure: sizeof


HOW to be safe: stdint.h


Mistakes (Steel-manned)


Active recall

Recall Forecast-then-Verify: before reading the table, what is

sizeof(long) on 64-bit Windows? 4 bytes (LLP64). On 64-bit Linux it's 8 (LP64). The whole point of the lesson.

Recall Why is

sizeof(char) == 1 always true? Because sizeof is defined to count in units of char. A char is "1 char" by definition, regardless of how many bits it has.

Recall Feynman: explain to a 12-year-old

Imagine boxes for holding numbers. C says "I'll give you a box, and it'll be at least this big, but on a small toy computer it might be smaller and on a big computer bigger." That's int. If you need a box that's exactly 4 slots wide no matter which computer — so two computers can mail boxes to each other and they line up perfectly — you ask for int32_t. sizeof is just a ruler that measures how many tiny slots a box uses.


Flashcards

What does sizeof measure and in what units?
Storage size of a type/object, in units of char (so sizeof(char)==1); result type is size_t.
Is sizeof a function or operator?
An operator, evaluated at compile time.
Minimum guaranteed width of int in C?
At least 16 bits.
What is the size ordering the standard guarantees?
char ≤ short ≤ int ≤ long ≤ long long.
On 64-bit Linux (LP64), sizeof(long)?
8 bytes.
On 64-bit Windows (LLP64), sizeof(long)?
4 bytes.
Which header gives int32_t, uint8_t, etc.?
<stdint.h>.
When should you use int32_t instead of int?
When you need an exact, platform-independent width (network packets, file formats, hardware registers).
Value range of a signed N-bit two's-complement integer?
2N1-2^{N-1} to 2N112^{N-1}-1.
Value of UINT32_MAX?
2321=42949672952^{32}-1 = 4294967295.
Correct printf specifier for sizeof?
%zu (size_t).
Why can sizeof(arr)/sizeof(arr[0]) fail in a function?
Array parameters decay to pointers, so sizeof returns pointer size, not array size.
Difference between int_least32_t and int_fast32_t?
least = smallest type ≥32 bits; fast = fastest type ≥32 bits.
Is plain char signed or unsigned?
Implementation-defined; use unsigned char/uint8_t for guaranteed 0–255.

Connections

  • Two's complement & integer overflow
  • Integer promotion & usual arithmetic conversions
  • Pointers and array decay
  • Struct padding & alignment
  • Endianness & serializing data
  • printf format specifiers
  • size_t and ptrdiff_t

Concept Map

guarantees only

leaves exact bytes

motivates

decided by

example

example

differ, causing

differ, causing

solved by

provides

measures at compile time

result type

yields

C standard

Minimum widths + ordering

Implementation-defined

1972 efficiency goal

Compiler + CPU + OS = ABI

LP64: long is 8 bytes

LLP64: long is 4 bytes

Portability bugs

stdint.h fixed widths

int32_t exact width

sizeof operator

Size in char units

size_t print with %zu

N bits

2^N distinct values

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, C language ek important cheez clearly nahi batati: int, long waghaira ka exact size kitna hoga. Woh sirf minimum guarantee deti hai (jaise int kam se kam 16-bit) aur ek ordering (char ≤ short ≤ int ≤ long). Asli size depend karta hai compiler + CPU + OS pe — isko ABI kehte hain. Isi wajah se same code different machine pe alag behave kar sakta hai. Classic trap: 64-bit Linux pe long = 8 bytes, lekin 64-bit Windows pe long = sirf 4 bytes. Agar aapne maan liya ki long hamesha 8 hai, to Windows pe bug aa jayega.

sizeof ek operator hai (function nahi), jo compile-time pe bata deta hai ki koi type kitne char-units leta hai. Isliye sizeof(char) hamesha 1 hota hai — kyunki measurement hi char ke units mein hoti hai. Print karte waqt %zu use karo, kyunki return type size_t hota hai (unsigned).

Jab aapko exact width chahiye — network packet, file format, hardware register — tab #include <stdint.h> karke int32_t, uint8_t, int64_t use karo. Inke naam mein hi number likha hai, to size guaranteed hai har platform pe. Mnemonic simple: jis type ke naam mein number hai (32, 64) uska size pakka; jiske naam mein number nahi (int, long) uska size pakka nahi.

Ek aur common galti: function ke andar sizeof(arr)/sizeof(arr[0]) se array length nikalna. Function mein array pointer ban jata hai (decay), to sizeof pointer ka size deta hai, array ka nahi — count galat aata hai. Isliye length alag se pass karo. Ye chhoti chhoti baatein hi 80% portability bugs bachati hain.

Go deeper — visual, from zero

Test yourself — C Programming