Site Tools


wiki:h-stdnoreturn

Table of Contents

<stdnoreturn.h>

<stdnoreturn.h> provides the noreturn macro (C11), which tells the compiler a function never returns—useful for die() or fatal() functions that always exit. This suppresses false warnings about missing returns and enables better dead-code elimination.

In C23, [[noreturn]] became the attribute syntax; the macro is the C11/C17 spelling.

Example

This example shows how noreturn suppresses the “control reaches end” warning.

// compile: gcc -Wall -o stdnoreturnexample stdnoreturnexample.c
// run: ./stdnoreturnexample
// description: noreturn suppresses control-flow warning
 
#include <stdnoreturn.h>
#include <stdio.h>
#include <stdlib.h>
 
noreturn void fail(const char* msg) {
    fprintf(stderr, "error: %s\n", msg);
    exit(1);
}
 
int safe_div(int a, int b) {
    if (b == 0) fail("divide by zero");
    return a / b;
}
 
int main() {
    printf("%d\n", safe_div(10, 2));
    return 0;
}
wiki/h-stdnoreturn.md · Last modified: by 127.0.0.1