C Reference ยท Keywords and Statements
The noreturn attribute
Reference for C noreturn: mark functions that never return with [[noreturn]] (C23) or _Noreturn (C11), effects on warnings and what is undefined.
A noreturn function never returns to its caller: it ends the program (exit, abort), jumps away (longjmp), or loops forever. Telling the compiler so lets it drop code after the call, avoid "control reaches end of non-void function" warnings, and optimize better.
C23 spells this with the attribute [[noreturn]] placed before the function declaration. C11 used the function specifier _Noreturn, with the macro noreturn in <stdnoreturn.h>; C23 marks that header and _Noreturn as obsolescent.
[[noreturn]] void fatal(const char *message);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
If a noreturn function does return, the behavior is undefined. This is shown read-only because the result is not stable:
[[noreturn]] void broken(void) { /* falls off the end: undefined behavior */ }
Syntax
[[noreturn]] return-type name(parameters); /* C23 */ _Noreturn return-type name(parameters); /* C11, C17 */ #include <stdnoreturn.h> /* macro noreturn for _Noreturn */
| Form | Description |
|---|---|
[[noreturn]] |
Standard attribute from C23, written at the start of a declaration. Also [[_Noreturn]]. |
_Noreturn |
Function specifier from C11. |
noreturn |
Macro for _Noreturn in <stdnoreturn.h>; obsolescent in C23. |
Returns: the function must not return. Its return type is usually void.
Errors / undefined behavior: if a function declared noreturn eventually returns (by return, or by reaching its end), the behavior is undefined; a compiler will often warn ("function declared 'noreturn' has a 'return' statement"). A noreturn function may still leave through exit, abort, longjmp, or a signal.
Usage notes
- Standard examples:
exit,_Exit,abort,quick_exit,thrd_exitandlongjmpare noreturn. - Warnings. After a noreturn call the compiler does not warn about a missing return value, as in
parse_digit. - Declare it on the prototype so callers in other files benefit; repeating it on the definition is allowed.
mainis not noreturn. The program may end inexitfrommain, butmainis expected to return.- Static analyzers use the attribute to prune impossible paths, which reduces false positives about null dereferences after
fatal(). - Ordinary exits are still possible with a loop
for (;;)that never ends; mark such functions noreturn too. - Attribute placement:
[[noreturn]]goes before the declaration specifiers, not after the parameter list. - Compilers. GCC supports
[[noreturn]]with-std=c23(and__attribute__((noreturn))always).
Related
- Attributes,
exit,abort,longjmp,return - Lessons: Error handling, Modern C