5.1.31 · HinglishC Programming

Compile-time assertions — static_assert

1,727 words8 min readRead in English

5.1.31 · Coding › C Programming


YE EXIST KYUN KARTA HAI?

Slogan: "Agar kuch compile time par jaana ja sakta hai, toh usse compile time par hi check karo."


YE HAI KYA EXACTLY?

Do historical forms jo aapko pehchanne chahiye:

Expression ek constant expression honi chahiye: literals, sizeof, enum values, #defined constants, etc. se bani — kuch bhi jo compiler program chalaye bina poori tarah evaluate kar sake.


COMPILER ISSE KAISE PROCESS KARTA HAI (first principles se derivation)

Hamare paas "derive" karne ka koi "formula" nahi hai, lekin hum behaviour ko derive kar sakte hain — constant expression kya hoti hai isse.

  1. Ek constant expression ki value translation time par known hoti hai (koi variables nahi, koi function calls nahi, koi runtime input nahi).
  2. Compiler compute karta hai.
  3. Rule define karo:
  1. Kyunki step 2 translation ke dauran hota hai, koi instruction generate nahi hoti — assertion ka final binary mein koi footprint nahi hota. Yahi poora point hai: yeh ek gatekeeper hai, runtime guard nahi.
Figure — Compile-time assertions — static_assert

static_assert vs assert vs #error

Kab check karta hai sizeof/enums dekhta hai? Runtime variables dekhta hai? Final binary mein?
#error preprocess no
static_assert compile no
assert run ✅ (aur bhi) yes (unless NDEBUG)

Common mistakes (steel-manned)


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

Socho tum ek Lego model bana rahe ho instructions se jo kahti hain "yeh base exactly 4 studs wide honi chahiye." Ek static_assert ek smart instruction sheet ki tarah hai jo tumhe build shuru hi nahi karne deti agar tumhari base 4 studs wide nahi hai — yeh tumhe setup par rok deti hai, na ki poori cheez banane ke baad jab woh gir jaaye. Check model exist hone se pehle hoti hai (compile time), toh ek baar model ban jaaye toh check ka koi cost nahi aur ise skip bhi nahi kiya ja sakta.


Active recall

When does static_assert evaluate its condition?
Compile time (translation) par, kabhi runtime mein nahi.
What must the condition of static_assert be?
Ek constant integer expression (koi runtime variables/function calls nahi).
What happens if the static_assert condition is false (zero)?
Compilation fail ho jaati hai aur message diagnostic ke roop mein print hoti hai.
What is the raw C11 keyword behind static_assert?
_Static_assert (static_assert ek macro hai <assert.h> mein).
How much runtime cost does a passing static_assert add?
Zero — koi machine code emit nahi hota.
In C11/C17, is the message argument optional?
Nahi, yeh mandatory hai; C23 mein yeh optional ban gaya.
Difference between static_assert and assert?
static_assert compile time par constants check karta hai; assert runtime par possibly-variable values check karta hai.
Why use static_assert instead of #error for sizeof checks?
#error preprocessor mein run hota hai jo sizeof evaluate nahi kar sakta; static_assert compiler mein run hota hai jo kar sakta hai.
Give a static_assert that enforces a power-of-two size N.
static_assert((N & (N-1)) == 0, "N must be power of two");
What disables assert but NOT static_assert?
NDEBUG define karna assert ko disable karta hai; static_assert unaffected rehta hai.

Connections

  • assert (runtime assertions)
  • Preprocessor directives — #error and #if
  • Constant expressions in C
  • sizeof operator
  • Enumerations (enum)
  • Header <assert.h>
  • C11 vs C23 language features

Concept Map

evaluates

known at

if

if

triggers

does nothing

zero footprint in binary

replaces

fires per run wasteful

motivates

macro form

native keyword

static_assert

Constant expression

Compile time check

Value is zero false

Value non-zero true

Compilation fails with message

Emits no code

Runtime assert

C11 keyword _Static_assert

C23 keyword message optional

Knowable at compile time