
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
_Noreturn function specifier in C
The _Noreturn function specifier in C indicates to the compiler that the function will either exit or run in an infinite loop. This function never returns the control to the main function or wherever it was called in the program. If the _Noreturn function uses the return statement, the compiler will generate a warning or show an undefined behavior.
Syntax of _Noreturn Function Specifier
The syntax of _Noreturn function specifier is given below:
_Noreturn data_type function_name() { --- code lines --- }
_Noreturn with exit() Function in C
If we use _Noreturn function specifie with a user-defined function that can contains the exit() function, it indicates to the compiler that the program will terminate without returning the control to the main() function where it is called.
Example
The following example demonstrates _Noreturn with the function containing exit():
#include <stdio.h> #include <stdlib.h> _Noreturn void example() { printf("This line will be executed inside the example() function.
"); // It will exit the program // without returning to the main() function exit(0); } int main() { printf("First statement of the main() printed.
"); example(); // Calling the noreturn function printf("It will never be printed.
"); return 0; }
The output of the above program is as follows:
First statement of the main() printed. This line will be executed inside the example() function.
_Noreturn with Infinite Loop
If we use the _Noreturn function specifier with a user-defined function that contains an infinite loop, it indicates to the compiler that the function will never return to the caller.
Example
The example below explains another use of _Noreturn to indicate to the compiler that the program will enter an infinite loop using for loop:
#include <stdio.h> #include <stdlib.h> _Noreturn void example() { printf("This line will be executed inside the example() function.
"); // It will exit the program // without returning to the main() function for (;;) { printf("This loop will never end will keep printing.
"); } } int main() { printf("First statement of the main() printed.
"); example(); // Calling the noreturn function printf("It will never be printed.
"); return 0; }
The output of the above program is as follows:
This loop will never end will keep printing. This loop will never end will keep printing. This loop will never end will keep printing. . . . Runs until infinity
_Noreturn with return Statement
If we use the _Noreturn function specifier with a user-defined function that contains a return statement, it violates the behavior of _Noreturn. Such functions must not return to its caller under any circumstance. Using a return statement in a function marked with _Noreturn results in undefined behavior.
Example
In this example, we have used a return statement in the example() function that throws a warning:
#include <stdio.h> #include <stdlib.h> _Noreturn void example() { printf("This line will be executed inside the example() function.
"); // It will exit the program // without returning to the main() function exit(0); // This return statement will throw a warning return 0; } int main() { printf("First statement of the main() printed.
"); example(); // Calling the noreturn function printf("It will never be printed.
"); return 0; }
The output of the above program is as follows:
main.c: In function 'example': main.c:12:12: warning: function declared 'noreturn' has a 'return' statement 12 | return 0; | ^ main.c:12:12: warning: 'return' with a value, in function returning void main.c:4:16: note: declared here 4 | _Noreturn void example() | ^~~~~~~ First statement of the main() printed. This line will be executed inside the example() function.
Conclusion
In this article, we discussed about _Noreturn function specifier in C and used three examples to understand its uses and behavior with the return statement.