<stdnoreturn.h> is a tiny C11 header that defines the macro noreturn as _Noreturn. Placing it on a function tells the compiler that the function never returns to its caller.
Typical examples are error handlers that call exit() or abort(). The hint lets the compiler skip “missing return” warnings and optimize the code after the call. C23 deprecates _Noreturn and this header in favor of the [[noreturn]] attribute.
Remember
#include <stdnoreturn.h>
noreturn void die(const char *msg);
die("fatal"); /* compiler knows control never comes back */
If a noreturn function does return -> undefined behavior
C23: prefer [[noreturn]]
Use
Prefer
Function that always exits
noreturn void f(void)
Function that loops forever
noreturn void forever(void)
C23 and newer
[[noreturn]] attribute
Skip the header
_Noreturn keyword directly (C11)
Approach
How to Use stdnoreturn.h in C
Put noreturn before the return type of a function that always exits.
Add #include <stdnoreturn.h> and compile with -std=c11.
Write noreturn before the return type: noreturn void die(...).
Make sure every path ends in exit, abort, longjmp, or an infinite loop.
Call the function from error paths such as a failed malloc.
In C23 code, write [[noreturn]] instead.
Piece
Required?
Role
#include <stdnoreturn.h>
C11
Defines the noreturn macro
noreturn before the type
Yes
Marks the function as never returning
A terminating path
Yes
Every path must exit, abort, jump away, or loop forever
Pseudocode
Pseudocode
noreturn void die(msg):
print msg to stderr
exit(EXIT_FAILURE) // control never leaves
int safe_div(a, b):
if b == 0: die("division by zero")
return a / b // no "missing return" warning
Reference
Cheat Sheet
Related spellings across C standards.
Name
Meaning
noreturn
Macro for _Noreturn from stdnoreturn.h
_Noreturn
The C11 function specifier
[[noreturn]]
C23 attribute, preferred in new code
exit, _Exit, abort, quick_exit
Standard functions that never return
longjmp
Also never returns to its caller
Trace
Worked Walkthrough — Why the Compiler Cares
Compare a function that can return with one the compiler knows cannot.
Calculation
noreturn void die(const char *msg) {
fprintf(stderr, "fatal: %s\n", msg);
exit(EXIT_FAILURE); <- control never leaves
}
int safe_div(int a, int b) {
if (b == 0) die("division by zero");
return a / b; <- the b == 0 path ends in die(), so no
} "control reaches end" warning
Code
C Examples
Compile with gcc file.c -std=c11 -o out.
Example 1 — Write a Fatal Error Helper
Print an error and exit. The compiler knows nothing runs after the call.