_Noreturn function specifier

Specifies that the function does not return to its point of invocation.

Syntax

_Noreturn function_declaration (since C11)

Explanation

The _Noreturn keyword appears in a function declaration and specifies that the function does not return by executing the return statement or by reaching the end of the function body (it may return by executing longjmp). If the function declared _Noreturn returns, the behavior is undefined. A compiler diagnostic is recommended if this can be detected.

The _Noreturn specifier may appear more than once in the same function declaration, the behavior is the same as if it appeared once.

This specifier is typically used through the convenience macro noreturn, which is provided in the header stdnoreturn.h.

Keywords

_Noreturn.

Standard library

The following functions are noreturn in the standard library:

Example

#include <stdlib.h>
#include <stdio.h>
#include <stdnoreturn.h>
 
// causes undefined behavior if i <= 0
// exits if i > 0
noreturn void stop_now(int i) // or _Noreturn void stop_now(int i)
{
    if (i > 0) exit(i);
}
 
int main(void)
{
  puts("Preparing to stop...");
  stop_now(2);
  puts("This code is never executed.");
}

Output:

Preparing to stop...

References

  • C11 standard (ISO/IEC 9899:2011):
    • 6.7.4 Function specifiers (p: 125-127)
    • 7.23 _Noreturn <stdnoreturn.h> (p: 361)

See Also

C++ documentation for [[noreturn]]
doc_C_Language
2016-10-10 18:36:51
Comments
Leave a Comment

Please login to continue.