control reaches end of non-void function

J

John Buckley

Hello!

I have a program like this:

#include <stdlib.h>

int main(void)
{
/* do stuff */

myExitFunc(0); /* very last statement in main() */
}

void myExitFunc(int ret)
{
exit(ret);
}

When I compile this, I get "warning: control reaches end of non-void
function" because there is no return statement in main. Question is, can I
safely ignore this warning or should I stick in a return anyway to pacify
the compiler?

Thanks
JB
 
J

Joona I Palaste

John Buckley said:
I have a program like this:
#include <stdlib.h>
int main(void)
{
/* do stuff */
myExitFunc(0); /* very last statement in main() */
}
void myExitFunc(int ret)
{
exit(ret);
}
When I compile this, I get "warning: control reaches end of non-void
function" because there is no return statement in main. Question is, can I
safely ignore this warning or should I stick in a return anyway to pacify
the compiler?

exit(ret) is the same as return ret *when in main()*. So you can safely
ignore this warning *for main()*. For other functions, do as your
compiler says.
 
E

Eric Sosman

John said:
Hello!

I have a program like this:

#include <stdlib.h>

int main(void)
{
/* do stuff */

myExitFunc(0); /* very last statement in main() */
}

void myExitFunc(int ret)
{
exit(ret);
}

When I compile this, I get "warning: control reaches end of non-void
function" because there is no return statement in main. Question is, can I
safely ignore this warning or should I stick in a return anyway to pacify
the compiler?

You're between a rock ...

Some compilers (gcc is one) provide language extensions
allowing you to declare that myExitFunc() does not return,
and this would silence the warning. But it would also make
your code dependent on the chosen compiler, and perhaps
limit its portability to other environments.

Adding a `return 0;' will probably silence the warning.
But it might also provoke new warnings from other, smarter
compilers that can figure out for themselves that myExitFunc()
never returns; they'll see an unreachable statement (the
added `return') and warn you that it'll never be executed.

.... and a hard place.
 
M

Martin Ambuhl

John said:
When I compile this, I get "warning: control reaches end of non-void
function" because there is no return statement in main. Question is, can I
safely ignore this warning or should I stick in a return anyway to pacify
the compiler?

The question of safety is tied to your start-up code and OS; the effect on
main itself is non-existant. You should return values from functions that
promise to return values for your own sense of consistency, not for the
compiler.
 
C

CBFalconer

Eric said:
You're between a rock ...

Some compilers (gcc is one) provide language extensions
allowing you to declare that myExitFunc() does not return,
and this would silence the warning. But it would also make
your code dependent on the chosen compiler, and perhaps
limit its portability to other environments.

Adding a `return 0;' will probably silence the warning.
But it might also provoke new warnings from other, smarter
compilers that can figure out for themselves that myExitFunc()
never returns; they'll see an unreachable statement (the
added `return') and warn you that it'll never be executed.

... and a hard place.

I recommend always putting in the 'return 0' in main. This
insulates the program from undefined behaviour if you later modify
myExitFunc. The only penalty for 'unreachable' code is (possibly)
a few extra instructions in the object module.
 
S

Sidney Cadot

Hi John,

John said:
When I compile this, I get "warning: control reaches end of non-void
function" because there is no return statement in main. Question is, can I
safely ignore this warning or should I stick in a return anyway to pacify
the compiler?

If you need this for doing cleanup, it may be interesting to know that
C99 supports the atexit() function (from stdlib.h) which allows you to
register functions to be executed at program termination.

Best regards,

Sidney Cadot
 
J

Jack Klein

P

Peter Nilsson

Sidney Cadot said:
If you need this for doing cleanup, it may be interesting to know that
C99 supports the atexit() function (from stdlib.h) which allows you to
register functions to be executed at program termination.

I think you were a bit trigger happy in stripping the context of the
original post, Sydney, but for the record, atexit() is available in
C90 too.
 
D

Dan Pop

In said:
I recommend always putting in the 'return 0' in main. This
insulates the program from undefined behaviour if you later modify
myExitFunc.

What undefined behaviour are you talking about?
The only penalty for 'unreachable' code is (possibly)
a few extra instructions in the object module.

And the diagnostic already mentioned by Eric.

This is a typical example showing why the C standard should have blessed
void main.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top