control reaches end of non-void function

Discussion in 'C Programming' started by John Buckley, Oct 13, 2003.

  1. John Buckley

    John Buckley Guest

    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
    John Buckley, Oct 13, 2003
    #1
    1. Advertising

  2. John Buckley <> scribbled the following:
    > 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?


    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.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Make money fast! Don't feed it!"
    - Anon
    Joona I Palaste, Oct 13, 2003
    #2
    1. Advertising

  3. John Buckley

    Eric Sosman Guest

    John Buckley wrote:
    >
    > 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.

    --
    Eric Sosman, Oct 13, 2003
    #3
  4. John Buckley wrote:

    > 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.


    --
    Martin Ambuhl
    Martin Ambuhl, Oct 13, 2003
    #4
  5. John Buckley

    CBFalconer Guest

    Eric Sosman wrote:
    > John Buckley wrote:
    > >
    > > #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.


    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.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Oct 13, 2003
    #5
  6. John Buckley

    Sidney Cadot Guest

    Hi John,

    John Buckley wrote:

    > 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
    Sidney Cadot, Oct 13, 2003
    #6
  7. John Buckley

    Jack Klein Guest

    On Tue, 14 Oct 2003 00:55:59 +0200, Sidney Cadot <>
    wrote in comp.lang.c:

    > Hi John,
    >
    > John Buckley wrote:
    >
    > > 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.


    And even more interestingly, so did C89.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Oct 14, 2003
    #7
  8. Sidney Cadot <> wrote in message news:<bmfahh$7r$>...
    > John Buckley wrote:
    >
    > > 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.


    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.

    --
    Peter
    Peter Nilsson, Oct 14, 2003
    #8
  9. John Buckley

    Dan Pop Guest

    In <> CBFalconer <> writes:

    >Eric Sosman wrote:
    >> John Buckley wrote:
    >> >
    >> > #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.

    >
    >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
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Oct 14, 2003
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ollej Reemt
    Replies:
    7
    Views:
    518
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    786
    The Real OS/2 Guy
    Oct 28, 2003
  3. Replies:
    5
    Views:
    824
    S.Tobias
    Jul 22, 2005
  4. Thomas Barth

    control reaches the end of non-void function

    Thomas Barth, Oct 24, 2005, in forum: C Programming
    Replies:
    8
    Views:
    3,726
    Christopher Benson-Manica
    Oct 24, 2005
  5. Replies:
    1
    Views:
    401
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page