Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The _Noreturn keyword is a function specifier introduced in the C11 standard that explicitly instructs the compiler that a function will never return control flow to its caller. It acts as a strict semantic guarantee, altering compiler code generation and static analysis.

Syntax and Declaration

The specifier is applied to function declarations and definitions. It can appear anywhere within the declaration specifiers, though it is conventionally placed at the beginning of the signature.
/* Standard C11 keyword usage */
_Noreturn void halt_execution(void);

/* Definition */
_Noreturn void halt_execution(void) {
    /* Implementation that must not return */
}
C11 also provides the <stdnoreturn.h> header, which defines the macro noreturn as a convenience alias for _Noreturn.
#include <stdnoreturn.h>

noreturn void halt_execution(void);

C23 Deprecation and Modern Standard

In the C23 standard, both the _Noreturn keyword and the <stdnoreturn.h> header were officially deprecated. Modern C aligns with C++ by utilizing the standard attribute syntax [[noreturn]]. Developers targeting C23 or later should use the attribute instead of the keyword.
/* C23 standard attribute replacement */
[[noreturn]] void halt_execution(void);

Semantic Rules and Undefined Behavior

  • Strict Non-Return Guarantee: If a function declared with _Noreturn (or [[noreturn]]) executes a return statement, or if control flow reaches the closing brace (}) of the function body, the program invokes undefined behavior (UB).
  • Return Type: While a non-returning function cannot yield a value to its caller, the C standard does not mandate that its return type be void. However, declaring it as void is the standard practice, as any specified return type is rendered meaningless.
  • Type System Independence: The _Noreturn specifier is a property of the function identifier, not part of the function’s type signature. Consequently, it cannot be used in the declaration of a function pointer.

Compiler Mechanics

When the compiler processes a non-returning specifier, it modifies its standard behavior in three primary ways:
  1. Warning Suppression: It suppresses static analysis warnings (e.g., “control reaches end of non-void function”) within the calling function, as the compiler recognizes that execution will not proceed past the call site.
  2. Dead Code Elimination: Any instructions sequentially following a call to a _Noreturn function are mathematically unreachable. The compiler marks this as dead code and strips it during the optimization phase.
  3. Stack and Call Site Optimization: At the call site, the compiler may omit post-call cleanup instructions, such as restoring caller-saved registers or popping arguments pushed to the stack. Furthermore, within the definition of the _Noreturn function itself, the compiler can omit the standard function epilogue (stack frame teardown and the ret instruction), as the return address pushed to the call stack will never be consumed.

Code Mechanics Example

#include <stdlib.h>
#include <stdnoreturn.h>

/* Valid: Control flow is handed off to exit(), which is also non-returning. */
noreturn void valid_noreturn(void) {
    exit(1); 
}

/* Invalid: Control flow reaches the end of the function body. 
   This strictly results in Undefined Behavior. */
noreturn void invalid_noreturn(void) {
    int x = 5;
    x++;
    /* UB triggered here: implicit return to caller */
}
Master C with Deep Grasping Methodology!Learn More