Tutorialspoint.dev

_Noreturn function specifier in C

After the removal of “noreturn” keyword, C11 standard (known as final draft) of C programming language introduce a new “_Noreturn” function specifier that specify that the function does not return to the function that it was called from. If the programmer try to return any value from that function which is declared as _Noreturn type, then the compiler automatically generates a compile time error.

// 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... ");
    view();
  
    printf("NOT over till now ");
    return 0;
}

Output:

Ready to begin...
After that abnormal termination of program.
compiler error:[Warning] function declared 'noreturn' has a '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... ");
    show();
  
    printf("NOT over till now ");
    return 0;
}

Output:

Ready to begin...
BYE BYE

Reference: http://en.cppreference.com/w/c/language/_Noreturn

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter