_Noreturn Function Specifier In C

_Noreturn Function Specifier In C

The _Noreturn keyword in a function declaration tells the compiler that the function will not return by executing a return statement or reaching the end of the function body. If a function is marked with _Noreturn does return, the behavior is undefined, and it’s a good practice for the compiler to provide a diagnostic message if it detects this situation.

You can use the _Noreturn specifier multiple times in the same function declaration, and it has the same effect as if it appeared only once.

Typically, you use this specifier through the convenience macro noreturn, which is available in the stdnoreturn.h header. It helps improve code readability and maintainability when specifying that a function does not return.

Note

_Noreturn function specifier is deprecated. [[noreturn]] attribute should be used instead.
The macro noreturn is also deprecated.

Example 1: To illustrate how_Noreturntype


// C program to show how _Noreturn type 
// function behave if it has return statement.
#include <stdio.h>
#include <stdlib.h>
 
// With return value
_Noreturn void view()
{
    return 10;
}
int main(void)
{
    printf("Ready to begin...\n");
    view();
 
    printf("NOT over till now\n");
    return 0;
}

Output

Ready to begin...
After that abnormal termination of program.
compiler error:[Warning] function declared 'noreturn' has a 'return' statement

Example 2: To illustrate the working of a No-return statement



// C program to illustrate the working 
// of _Noreturn type function.
#include <stdio.h>
#include <stdlib.h>
 
// Nothing to return
_Noreturn void show()
{
    printf("BYE BYE");
}
int main(void)
{
    printf("Ready to begin...\n");
    show();
 
    printf("NOT over till now\n");
    return 0;
}

Output

Ready to begin...
BYE BYE

FAQ- _Noreturn function specifier in C

Q1. What is the Noreturn function specifier in C?

Ans. In C, a function specifier is like a label that tells the compiler how a function should behave. When you use the inline function specifier, you’re suggesting to the compiler that it should replace each call to that function with the actual code inside the function. This can help improve performance by reducing the overhead of function calls.

Q2. What are non-returning functions C++?

Ans. A non-value returning function, or void function in C, doesn’t provide a result. It automatically returns to the caller at the end of the function, and you don’t need a return statement.

Q3. Does the Noreturn function return?

Ans. In C, the _Noreturn keyword in a function declaration means that the function doesn’t return via a return statement or by reaching the end of its body (though it can return via longjmp). If a function is marked as _Noreturn does return under normal circumstances, the behavior is undefined.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment