Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why a warning of "control reaches end of non-void function" for the main function?

Tags:

I run the following C codes and got a warning: control reaches end of non-void function

int main(void) {} 

Any suggestions?

like image 889
alittleboy Avatar asked Nov 19 '12 23:11

alittleboy


People also ask

What does Warning control reaches end of non-void function?

After executing programs, sometimes we get the error: 'warning: control reaches the end of non-void function', which means that certain functions that would have to return some values attain the termination. It might not give any value later.

What is a non-void function?

Functions that return are great for when we need to go through a lot of steps to get a value that we want to use in our code somewhere. Calling a non-void function tells the computer to head over to the function definition, do everything, and come back with the result once it's done.

What happens if you forget the return statement in a non-void function?

If control reaches the closing curly brace ( } ) of a non- void function without evaluating a return statement, using the return value of the function call is undefined behavior.

Why control reaches end of non-void function for the main function?

- Config Router Why a warning of “control reaches end of non-void function” for the main function? Just put return 0 in your main (). Your function main returns an int (int main (void)) therefore you should add a return in the end of it. Solution: This warning is similar to the warning described in Return with no value.

Why does my function main have no return value?

Your function main returns an int (int main (void)) therefore you should add a return in the end of it. Solution: This warning is similar to the warning described in Return with no value. If control reaches the end of a function and no return is encountered, GCC assumes a return with no return value.

How do you end a function that returns a void?

Syntatically, return is the right way to end a function that returns void. However, stylistically, a return in middle of a function looks inelegant. It’s like one of those people who just get up from a middle of a conversation and leave… how rude.

What happens when a non-void function returns a negative value?

As you can see, if a is negative, then the function will return -a, but if a is positive or zero, then nothing is returned and the compiler will complain with “control reaches end of non-void function”). The reason it tells “non-void” is because “void” is the keyword for functions that do not return any value, as this one:


2 Answers

Just put return 0 in your main(). Your function main returns an int (int main(void)) therefore you should add a return in the end of it.

Control reaches the end of a non-void function

Problem: I received the following warning: 

warning: control reaches end of non-void function

Solution: This warning is similar to the warning described in Return with no value. If control reaches the end of a function and no return is encountered, GCC assumes a return with no return value. However, for this, the function requires a return value. At the end of the function, add a return statement that returns a suitable return value, even if control never reaches there.

source

Solution:

int main(void) {     my_strcpy(strB, strA);     puts(strB);     return 0; } 
like image 88
dreamcrash Avatar answered Oct 05 '22 12:10

dreamcrash


As an alternative to the obvious solution of adding a return statement to main(), you can use a C99 compiler (“gcc -std=c99” if you are using GCC).

In C99 it is legal for main() not to have a return statement, and then the final } implicitly returns 0.

$ gcc -c -Wall t.c t.c: In function ‘main’: t.c:20: warning: control reaches end of non-void function $ gcc -c -Wall -std=c99 t.c $  

A note that purists would consider important: you should not fix the warning by declaring main() as returning type void.

like image 33
Pascal Cuoq Avatar answered Oct 05 '22 12:10

Pascal Cuoq