5.1.31 · D3 · HinglishC Programming

Worked examplesCompile-time assertions — static_assert

3,265 words15 min read↑ Read in English

5.1.31 · D3 · Coding › C Programming › Compile-time assertions — static_assert

Aapne parent note padh liya hai aur ek-line rule jaante hain: static_assert(condition, "message") compiler ko build refuse karne par majboor kar deta hai agar condition compile time par zero evaluate ho. Yeh page theory ka ulta hai — yeh har tarah ke case grind karta hai jo yeh tool aap par throw kar sakta hai, taaki jab aap ise real mein dekho toh aap uska twin pehle hi dekh chuke ho.

Kisi bhi example se pehle: condition ek constant expression hai (dekho Constant expressions in C). Matlab compiler iske liye ek fixed number compute kar sakta hai — program run kiye bina. Phir:


Scenario matrix

static_assert ko ek machine samjho jo constant khaati hai aur pass ya fail output deti hai. "Cases" woh sab shapes hain jo constant le sakti hai. Yeh har cell hai jo hum cover karenge.

Cell Case class Representative question
A Truthy value (koi bhi non-zero → pass) sizeof(int) == 4 ek 32-bit box par
B Falsy value (exactly zero → fail) wahi check ek 16-bit box par
C Degenerate: bare non-zero literal static_assert(1) — kya yeh pass bhi hota hai?
D Zero-produce karne wala arithmetic power-of-two mask x & (x-1)
E Do cheezein sync mein rakhna array length vs enum count
F Boundary / limiting value sabse chhoti legal size, x = 1, x = 0
G Negative & signedness twist -1 truthy hai; unsigned wrap
H Real-world word problem packet header ek fixed wire size mein fit hona chahiye
I Exam twist inme se kaun si constant expression nahi hai?

Neeche har example cell(s) ke saath tagged hai jise woh cover karta hai. Milke yeh har row ko touch karte hain.


Cell A + C — truthy values aur degenerate literal


Cell B — woh falsy value jo build rok deti hai

Figure — Compile-time assertions — static_assert

Cell D + F — arithmetic jo zero produce karta hai, aur boundary values

Classic "power of two hona chahiye" test mask use karta hai, jahan & bitwise AND hai. Yeh raha kyun kaam karta hai, binary mein, isse assert karne se pehle.

Figure — Compile-time assertions — static_assert

Cell E — do definitions ko sync mein rakhna


Cell G — negatives aur signedness twist


Cell H — real-world word problem


Cell I — exam twist: constant expression kya nahi hai?


Coverage check — kya humne har cell hit kiya?

Recall Matrix cells vs examples (sab nine confirm)
  • A truthy value → Example 1 (1, 42, sizeof(char) sab non-zero)
  • B falsy value → Example 2 (sizeof(int)==4 16-bit box par deta hai)
  • C bare non-zero literal → Example 1 (static_assert(1) passes)
  • D zero-producing arithmetic → Example 3 (power-of-two mask)
  • E do cheezein sync mein → Example 4 (array length vs COLOR_COUNT)
  • F boundary / degenerate → Example 3 ( aur cases)
  • G negative & signedness twist → Example 5 (-1 truthy; unsigned wrap)
  • H real-world word problem → Example 6 (8-byte packet header)
  • I exam twist → Example 7 (kaun si condition constant expression nahi hai)

Sare nine cells A–I cover ho gaye — koi scenario nahi chhuta.

Active recall

Kya static_assert(-1) pass hota hai?
Haan — C mein truthy hai.
Kya (x & (x-1)) == 0 sahi se x = 0 reject karta hai?
Nahi — yeh ke liye true hai, toh x != 0 add karo.
Kya sizeof(n) == 4 static_assert mein valid hai even if n ek variable hai?
Haan — sizeof type use karta hai, jo compile time par resolve hota hai.
get_count() == 4 static_assert mein illegal kyun hai?
Function call ek runtime value hai, constant expression nahi.
1000 ke liye, 1000 & 999 kya hai?
992 (non-zero), toh power-of-two assert fail hoti hai.
C11/C17 mein static_assert spelling kaun sa header deta hai?
<assert.h> (ya raw _Static_assert keyword use karo bina include ke).