Modern C (C11/C17/C23)

_Noreturn / [ [noreturn]]

/ NOH-ree-turn /

Most functions, when called, eventually return to the caller. But a few never do: abort() kills the program, exit() terminates it, a function that loops forever, or one that always calls another non-returning function. Telling the compiler that a function never returns lets it generate better code and give better warnings. That declaration is what _Noreturn (C11) and the C23 attribute spelling express.

You mark the function in its declaration: _Noreturn void die(const char *msg); (or, friendlier, noreturn from <stdnoreturn.h>), and in C23 you use the attribute form written with double brackets, placed before the function. The promise is that control will never flow back to the caller from this function. Because the compiler now knows code after a call to die() is unreachable, it can omit warnings about a 'missing return' on a path that ends in die(), drop dead code, and avoid setting up a normal return.

It matters for fatal-error helpers, assertion failures, and longjmp-style escapes, making intent explicit and silencing spurious warnings. The sharp caveat: this is a PROMISE, not a check — if a function marked _Noreturn actually does return (say it falls off the end), the behavior is undefined, and the optimizer may have already deleted the code that would have run afterward. Note also that C23 deprecates the _Noreturn keyword form in favor of the attribute.

#include <stdlib.h> [ [noreturn]] void die(const char *msg) { // C23 attribute form fputs(msg, stderr); exit(1); // control never returns } int f(int x) { if (x < 0) die("bad"); return x; } // no missing-return warning

Marking die as non-returning lets the compiler treat code after a die() call as unreachable and skip the missing-return warning.

It is a promise, not a guarantee the compiler checks: if a _Noreturn function actually returns, the behavior is undefined — and C23 deprecates the _Noreturn keyword in favor of the attribute spelling.

Also called
noreturnnon-returning function不回傳函式