C _Noreturn Keyword
Example
Create a function that ends the program with exit():
_Noreturn void stopProgram() {
printf("Stopping the program");
exit(0);
}
int main() {
stopProgram();
printf("This line is never printed");
return 0;
}
Definition and Usage
The _Noreturn keyword is used in front of a function. It tells the compiler that the function never returns to the code that called it, for example because it ends the program with exit().
This lets the compiler make the code faster, and warn about some mistakes. If a _Noreturn function does return, the result is undefined (the program can behave in unexpected ways).
If you include <stdnoreturn.h>, you can write noreturn instead of _Noreturn.
Related Pages
Read more about functions in our C Functions Tutorial.
Read more about exit() in our C Error Handling Tutorial.