5.1.3 · Coding › C Programming
C tumhe int, long, etc. ke liye exact sizes guarantee nahi karta . Sirf minimum ranges aur ek ordering guarantee karta hai. Actual number of bytes depend karta hai compiler + CPU + OS (yaani "ABI") par. Agar tumhare code ko exact width chahiye (network packets, file formats, hardware registers), toh ==stdint.h== ke fixed-width types use karo jaise int32_t. Yahi woh 20% hai jo 80% portability bugs rokta hai.
C 1972 mein design kiya gaya tha taaki jis bhi machine par ho, efficiently run kar sake . Ek 16-bit machine, 32-bit machine, aur 64-bit machine mein se har ek ka ek "natural word size" hota hai jise CPU sabse fast handle karta hai. Yeh kehke ki "int natural fast integer hai, kam se kam 16 bits" , C compiler ko allow karta hai ki woh us hardware ke liye sabse fast size choose kare, instead of har jagah ek slow fixed size force karne ke. Cost yeh hai: tum assume nahi kar sakte ki int 4 bytes ka hai.
C standard minimum widths aur ek size ordering guarantee karta hai, exact bytes nahi:
char ≥ 8 bits (aur sizeof(char) == 1 hamesha , by definition).
short ≥ 16 bits, int ≥ 16 bits, long ≥ 32 bits, long long ≥ 64 bits.
Ordering: sizeof(char) ≤ sizeof(short) ≤ sizeof(int) ≤ sizeof(long) ≤ sizeof(long long) .
Baaki sab implementation-defined hai (compiler/ABI dwara choose kiya jaata hai).
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
long 64-bit Linux par 8 bytes ka hai lekin 64-bit Windows par 4 bytes ka. Woh code jo assume karta hai sizeof(long)==8, silently break ho jaata hai jab move kiya jaata hai. Exactly isliye fixed-width types exist karte hain.
sizeof
==sizeof== ek compile-time operator hai (function nahi) jo kisi type ya object ki storage size char ke units mein yield karta hai (yaani "kitne chars lagte hain iske liye"). Iska result type ==size_t== hai (ek unsigned type), isliye ise %zu se print karo.
Worked example Apni machine par measure karna
#include <stdio.h>
int main ( void ) {
printf ( "char = %zu\n " , sizeof ( char ));
printf ( "int = %zu\n " , sizeof ( int ));
printf ( "long = %zu\n " , sizeof ( long ));
printf ( "ptr = %zu\n " , sizeof ( void* ));
int a [ 10 ];
printf ( "array = %zu , n= %zu\n " , sizeof a, sizeof a / sizeof a [ 0 ]);
return 0 ;
}
sizeof a / sizeof a[0] kyun? Total bytes ÷ bytes-per-element = element count — yeh array length paane ka ek portable tarika hai jo int size par depend nahi karta . (Warning: sirf real arrays par kaam karta hai, pointer parameter par nahi , jahan array "decay" ho chuka hota hai.)
Definition Fixed-width types
#include <stdint.h> tumhe exact bit count wale types deta hai, guaranteed across platforms:
==int8_t, int16_t, int32_t, int64_t== — exactly utne hi bits, signed, two's complement.
uint8_t … uint64_t — unsigned versions.
intptr_t / uintptr_t — itna bada integer ki ek pointer hold ho sake.
int_least32_t (≥32, sabse chhota available) aur int_fast32_t (≥32, sabse fast available) jab exact size ki zaroorat nahi ho lekin minimum chahiye.
Worked example Network / file format struct
#include <stdint.h>
typedef struct {
uint16_t port; // exactly 2 bytes, every platform
uint32_t ip; // exactly 4 bytes
int64_t timestamp; // exactly 8 bytes
} Packet;
Yeh step kyun? Wire par ek packet ka ek fixed byte layout hota hai. Agar tum int/long use karte, toh struct ke bytes Windows aur Linux mein alag hote aur do machines baat nahi kar paatein. int32_t layout ko nail kar deta hai.
int hamesha 4 bytes ka hota hai."
Kyun sahi lagta hai: Aaj jo bhi desktop tum touch karte ho, uspar hota hi 4 hai. Fix: yeh mainstream 32/64-bit ABIs ka coincidence hai; embedded/16-bit targets mein 2-byte int hota hai. Jab actually 4 chahiye toh int32_t use karo.
long 64-bit par hamesha 8 hota hai."
Kyun sahi lagta hai: 64-bit Linux/macOS par sach hai. Fix: 64-bit Windows LLP64 hai , jahan long 4 ka hota hai. Kabhi bhi long width assume mat karo — int64_t/intptr_t use karo.
sizeof ko %d se print karna.
Kyun sahi lagta hai: Chhote numbers ke liye %d "kaam" karta hai. Fix: sizeof size_t return karta hai (unsigned, possibly 64-bit). Mismatched format specifiers undefined behavior hai; %zu use karo.
Common mistake Kisi function ke andar
sizeof(arr)/sizeof(arr[0]) jo array parameter leti hai.
Kyun sahi lagta hai: Working version se bilkul same lagta hai. Fix: array parameters pointers mein decay ho jaate hain, isliye sizeof(arr) pointer size hai, array ki nahi. Count galat aata hai. Length explicitly pass karo.
char defaults signed hota hai.
Kyun sahi lagta hai: x86 par char signed hota hai. Fix: plain char ki signedness implementation-defined hai; agar tumhe 0 –255 byte value chahiye toh unsigned char / uint8_t use karo.
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.
Mnemonic Safe choice yaad rakhne ke liye
"Exact chahiye? Number pick karo." int32_t mein literally 32 likha hai — naam padho, size milo. Plain int/long mein koi number nahi , isliye unka koi promise nahi .
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? − 2 N − 1 to 2 N − 1 − 1 .
UINT32_MAX ki value?2 32 − 1 = 4294967295 .
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.
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
exact bytes chhod deta hai
differ hoke cause karta hai
differ hoke cause karta hai
compile time par measure karta hai
Minimum widths + ordering
Compiler + CPU + OS = ABI