A static assertion checks a constant expression at compile time. If the check fails, the program does not build and the compiler prints your message. C11 provides the keyword _Static_assert, and <assert.h> defines the friendlier macro static_assert. It complements the runtime assert() macro.
Put a constant condition and a message wherever a declaration is allowed.
Compile with C11 or newer: gcc file.c -std=c11 -o out.
Include <assert.h> to use the static_assert spelling (or use _Static_assert directly).
Write a condition built from constants, sizeof, offsetof, or CHAR_BIT.
Add a short message that explains what the build assumes.
Place it at file scope, in a struct, or inside a function.
Part
Required?
Role
Constant expression
Yes
Evaluated by the compiler
Message string
C11: yes, C23: optional
Shown when the check fails
<assert.h>
For the static_assert macro in C11/C17
Maps to _Static_assert
Pseudocode
Pseudocode
assumption = some compile-time fact (size, offset, constant)
static_assert(assumption, "why it must hold")
build succeeds only when the assumption is true
Reference
Cheat Sheet
Use static assertions to turn silent assumptions into build errors.
Topic
Detail
Standard
C11 (_Static_assert); C23 makes static_assert a keyword
Condition
Integer constant expression, non-zero means pass
Allowed places
File scope, block scope, and inside struct declarations
Common checks
sizeof, offsetof, CHAR_BIT, enum values
Cannot check
Values known only at run time (use assert)
Message
Plain string literal
Trace
Worked Walkthrough — A Failing Check
Consider static_assert(sizeof(int) >= 8, "int must be 64 bits"); on a platform where int is 4 bytes.
Calculation
1) Compiler evaluates sizeof(int) -> 4
2) Compiler evaluates 4 >= 8 -> 0 (false)
3) Condition is false -> diagnostic issued
4) Compilation stops, message is printed
error: static assertion failed: "int must be 64 bits"
No object file is produced, so the bug never reaches run time.
Code
C Examples
Compile with gcc file.c -std=c11 -o out. For run-time checks see assert.h; for layouts see offsetof.
Example 1 — Require a Minimum int Size
Fail the build early if a type is smaller than your code assumes.
C
#include <assert.h>
#include <stdio.h>
static_assert(sizeof(int) >= 4, "int must be at least 32 bits");
int main(void) {
printf("sizeof(int) = %zu\n", sizeof(int));
return 0;
}
Output (typical)
sizeof(int) = 4
How It Works
File scope. The check lives outside any function.
Constant expression.sizeof(int) >= 4 is known at compile time.
Passes. No code is generated for the assertion.
Program runs. It prints the actual size.
Example 2 — What a Failed Check Looks Like
Change the condition to something false and the build stops with your message.
C
#include <assert.h>
static_assert(sizeof(int) >= 8, "int must be at least 64 bits");
int main(void) {
return 0;
}
Compiler output (GCC style)
static_fail.c:3:1: error: static assertion failed: "int must be at least 64 bits"
How It Works
False condition. A 4-byte int is not at least 8 bytes.
Build error. The compiler stops before producing an executable.
Message shown. Your text explains the broken assumption.
Wording varies. Exact diagnostics differ between GCC, Clang, and MSVC.
Example 3 — Guard a Struct Layout with offsetof
Lock in the layout of a struct that is shared with other code or files.
C
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
struct Packet {
uint16_t id;
uint16_t len;
uint8_t payload[8];
};
static_assert(offsetof(struct Packet, payload) == 4,
"payload must start at byte 4");
static_assert(sizeof(struct Packet) == 12,
"Packet must be 12 bytes");
int main(void) {
printf("sizeof(struct Packet) = %zu\n", sizeof(struct Packet));
return 0;
}
Output
sizeof(struct Packet) = 12
How It Works
Layout. Two 16-bit fields (4 bytes) then 8 payload bytes, with no padding on common ABIs.
offsetof is constant. It can be used inside a static assertion.
Future-proof. Reordering fields or adding a larger member breaks the build instead of the protocol.
Static assertions are simple, but a few rules trip people up.
constant
Condition must be constant
A variable or function result is rejected. Use assert() for run-time values.
standard
Needs C11 or later
Older compilers (C99 and earlier) lack it. Compile with -std=c11 or newer.
include
Forgetting assert.h
Without <assert.h> in C11/C17, static_assert is undefined. _Static_assert still works.
message
Missing message in C11
C11 requires the message string. C23 makes it optional.
compat
Used in headers
Static assertions are safe to put in headers; they add no run-time code.
Analysis
Performance Notes
Aspect
Cost
Notes
Run time
None
Nothing is generated
Compile time
Negligible
One constant evaluation
Failure
Build error
Caught before shipping
Prefer static assertions over comments for assumptions about type sizes, struct layouts, and enum ranges. They cost nothing at run time and cannot go stale silently.
Remember
Key Takeaways
Timing: checked at compile time, with no run-time cost.
Syntax:static_assert(cond, "msg"); or _Static_assert.
Input: the condition must be a constant expression.
Use: guard sizes, offsets, and platform assumptions.
One line: turn assumptions into build errors with static_assert.
Frequently Asked Questions
A compile-time check introduced in C11 as _Static_assert. The assert.h header provides the static_assert macro. If the constant condition is false, compilation fails.
assert() runs while the program executes and can use any value. static_assert is evaluated by the compiler and needs a constant expression.
For the static_assert spelling in C11 and C17, yes. In C23 static_assert is a keyword. _Static_assert needs no header.
No. It generates no code, so the compiled program is unchanged.
Yes. Both are constant expressions, so they are ideal for checking sizes and struct layouts.
In C11 and C17, yes. C23 allows omitting the message.