C stdnoreturn.h Header

Beginner
6 min read
Updated: Nov 2026
3 examples
C11

What Is stdnoreturn.h?

<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]]
UsePrefer
Function that always exitsnoreturn void f(void)
Function that loops forevernoreturn void forever(void)
C23 and newer[[noreturn]] attribute
Skip the header_Noreturn keyword directly (C11)

How to Use stdnoreturn.h in C

Put noreturn before the return type of a function that always exits.

  1. Add #include <stdnoreturn.h> and compile with -std=c11.
  2. Write noreturn before the return type: noreturn void die(...).
  3. Make sure every path ends in exit, abort, longjmp, or an infinite loop.
  4. Call the function from error paths such as a failed malloc.
  5. In C23 code, write [[noreturn]] instead.
PieceRequired?Role
#include <stdnoreturn.h>C11Defines the noreturn macro
noreturn before the typeYesMarks the function as never returning
A terminating pathYesEvery 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

Cheat Sheet

Related spellings across C standards.

NameMeaning
noreturnMacro for _Noreturn from stdnoreturn.h
_NoreturnThe C11 function specifier
[[noreturn]]C23 attribute, preferred in new code
exit, _Exit, abort, quick_exitStandard functions that never return
longjmpAlso never returns to its caller

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

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.

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

noreturn void fatal(const char *msg) {
    fprintf(stderr, "fatal: %s\n", msg);
    exit(EXIT_FAILURE);
}

int main(void) {
    printf("starting\n");
    fatal("disk full");
}

How It Works

  1. Mark the helper. noreturn promises that fatal never comes back.
  2. End with exit. exit(EXIT_FAILURE) terminates the program, so the promise holds.
  3. No return needed in main. Control never reaches the end of main after fatal.

Example 2 — Guard a Function with a Noreturn Helper

Use a noreturn helper on an error path inside a function that otherwise returns a value.

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

static noreturn void die(const char *msg) {
    fprintf(stderr, "fatal: %s\n", msg);
    exit(EXIT_FAILURE);
}

static int safe_div(int a, int b) {
    if (b == 0) {
        die("division by zero");
    }
    return a / b;
}

int main(void) {
    printf("10 / 2 = %d\n", safe_div(10, 2));
    printf("1 / 0 = %d\n", safe_div(1, 0));
    return 0;
}

How It Works

  1. Normal path. safe_div(10, 2) returns 5.
  2. Error path. b == 0 calls die, which exits.
  3. Second printf never completes. The program ends inside die before printing a result.

Example 3 — Quit Cleanly with atexit

A noreturn function can still run registered cleanup handlers through exit().

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

static void goodbye(void) {
    printf("cleanup done\n");
}

static noreturn void quit(int code) {
    printf("quitting with code %d\n", code);
    exit(code);
}

int main(void) {
    atexit(goodbye);
    printf("running\n");
    quit(0);
}

How It Works

  1. Register cleanup. atexit(goodbye) schedules goodbye to run at exit.
  2. Call quit. It prints a message, then calls exit.
  3. Handlers run. exit runs goodbye before the process ends. (_Exit would skip it.)

Edge Cases & Pitfalls

A wrong noreturn promise is undefined behavior.

ub

Returning is undefined

If a noreturn function returns, by reaching its end or by a return statement, the behavior is undefined.

c23

C23 deprecates it

Use [[noreturn]] in new C23 code. _Noreturn and this header are obsolescent.

order

Write it with the specifiers

Place noreturn before the return type, as in noreturn void f(void), not after the parameter list.

warn

Compilers check it

GCC and Clang warn when a function marked noreturn can return.

Performance & Portability Notes

ApproachCostBest for
noreturn functionNo run-time costError and exit handlers
Code after the callThe compiler may drop itSmaller, faster code
Static analysisTools prune impossible pathsFewer false warnings

noreturn is a promise to the compiler, not a feature that forces termination. The function body must still end the program.

Key Takeaways

  • Role: mark functions that never return to the caller.
  • Spelling: noreturn from this header, _Noreturn in C11, [[noreturn]] in C23.
  • Rule: every path must exit, abort, jump, or loop forever.
  • Benefit: fewer warnings and better optimization.

One line: stdnoreturn.h lets you promise the compiler that a function never comes back.

Frequently Asked Questions

stdnoreturn.h is a C11 header that defines the noreturn macro as _Noreturn, marking a function that never returns to its caller.
The behavior is undefined. Make sure every path ends in exit, abort, longjmp, or an infinite loop.
Yes. noreturn is a macro from stdnoreturn.h that expands to _Noreturn.
In C23 _Noreturn and stdnoreturn.h are obsolescent. The preferred spelling is the [[noreturn]] attribute.
exit, _Exit, abort, and quick_exit never return, and longjmp does not return to its caller.

Next: string.h

Copy, compare, and search strings with strlen, strcpy, and strcmp.

string.h tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful