Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Noreturn function specifier in C
3.
Example
3.1.
Example 1
3.2.
Example 2
4.
Frequently Asked Questions
4.1.
What is the "noreturn" function in the C programming language?
4.2.
Why does the noreturn function return an error when calling it?
4.3.
Why does the function noreturn() not return any value?
5.
Conclusion
Last Updated: Mar 27, 2024

Noreturn function specifier in C

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

In this blog, we will learn about the Noreturn function specifier in C. We will start with a brief introduction to the Noreturn function specifier. Then, we will get to see the working of the Noreturn function specifier in C with the help of some examples.

Also Read About, Sum of Digits in C, C Static Function.

Noreturn function specifier in C

In the C11 standard, the “noreturn” keyword is removed, and it presents a new "_Noreturn" function specifier that indicates that the function does not return to the function from which it was called. The compiler creates a compile-time error if the programmer tries to return any value from a function designated as _Noreturn type.

Syntax

_Noreturn void Function_Name() {
// function body
}


Recommended topic, Tribonacci Series and  Short int in C Programming

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Example

Now, let’s see some examples of how to use the”_Noreturn” specifier.

Example 1

#include<stdio.h>
int function() {
   return 100; 
}
main() {
   printf("The returned value: %d\n", function());
}

Output

The returned value: 100

In this example, we have created a function and returned a value of that function.

Example 2

#include <stdio.h>
#include <stdlib.h>


_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…
Warning: function declared ‘nonreturn’ has a ‘return’ statement.

We have created a noretrun function to return an integer value in this example. The main function is created to call the view function since it will show compile error that noreturn has a return statement. You can practice by yourself on an online C compiler.

Frequently Asked Questions

What is the "noreturn" function in the C programming language?

It is a compiler limitation that tells the compiler that "MY code will never return." 

Why does the noreturn function return an error when calling it?

When a noreturn function is used, a compiler sends a warning converted into an error message. 

Why does the function noreturn() not return any value?

Noreturn does not tell the compiler that functions don't return any value. It tells the compiler that the control flow will not return to the caller.

Conclusion

In this article, we have discussed the noreturn function specifier in C. We also discussed its syntax and several examples and their outputs and explanations.

To learn more, see Operating SystemUnix File SystemFile System Routingand File Input/Output.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Previous article
Function in C Programming Examples
Next article
memcpy() in C
Live masterclass