5.1.3 · HinglishC Programming

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

1,697 words8 min readRead in English

5.1.3 · Coding › C Programming


C sizes unspecified kyun rakhta hai?


Standard actually kya guarantee karta hai


Common ABIs (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 (purana 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: table padhne se pehle, 64-bit Windows par

sizeof(long) kya hoga? 4 bytes (LLP64). 64-bit Linux par 8 hota hai (LP64). Yahi is lesson ka poora point hai.

Recall

sizeof(char) == 1 hamesha true kyun hai? Kyunki sizeof defined hai char ke units mein count karne ke liye. Ek char definition se "1 char" hota hai, chahe usmein kitne bhi bits hon.

Recall Feynman: ek 12-saal ke bachche ko explain karo

Socho numbers rakhne ke liye boxes hain. C kehta hai "main tumhe ek box dunga, aur woh kam se kam itna bada hoga, lekin ek chhote toy computer par chhota ho sakta hai aur bade computer par bada." Yahi int hai. Agar tumhe ek box chahiye jo exactly 4 slots wide ho chahe koi bhi computer ho — taaki do computers ek dusre ko boxes mail kar sakein aur woh bilkul fit ho jaayein — toh tum int32_t maangoge. sizeof bas ek ruler hai jo measure karta hai ki ek box kitne tiny slots use karta hai.


Flashcards

sizeof kya measure karta hai aur kis unit mein?
Kisi type/object ki storage size, char ke units mein (toh sizeof(char)==1); result type size_t hai.
sizeof ek function hai ya operator?
Ek operator hai, compile time par evaluate hota hai.
C mein int ki minimum guaranteed width kya hai?
Kam se kam 16 bits.
Standard jo size ordering guarantee karta hai woh kya hai?
char ≤ short ≤ int ≤ long ≤ long long.
64-bit Linux (LP64) par sizeof(long)?
8 bytes.
64-bit Windows (LLP64) par sizeof(long)?
4 bytes.
Kaun sa header int32_t, uint8_t, etc. deta hai?
<stdint.h>.
int ki jagah int32_t kab use karna chahiye?
Jab tumhe exact, platform-independent width chahiye (network packets, file formats, hardware registers).
Signed N-bit two's-complement integer ki value range?
to .
UINT32_MAX ki value?
.
sizeof ke liye correct printf specifier kya hai?
%zu (size_t).
sizeof(arr)/sizeof(arr[0]) kisi function mein fail kyun ho sakta hai?
Array parameters pointers mein decay ho jaate hain, isliye sizeof pointer size return karta hai, array size nahi.
int_least32_t aur int_fast32_t mein kya difference hai?
least = ≥32 bits ka sabse chhota type; fast = ≥32 bits ka sabse fast type.
Plain char signed hai ya unsigned?
Implementation-defined; guaranteed 0–255 ke liye unsigned char/uint8_t use karo.

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

sirf guarantee karta hai

exact bytes chhod deta hai

motivate karta hai

decide hota hai

example

example

differ hoke cause karta hai

differ hoke cause karta hai

solve hote hain

provide karta hai

compile time par measure karta hai

result type

yield karta hai

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