I run the following C codes and got a warning: control reaches end of non-void function
int main(void) {}
Any suggestions?
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.
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.
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.
- 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.
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.
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.
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:
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; }
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With