exit()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Darklight

    exit()

    if the exit() function is contained in stdlid.h
    why if i remove the above header file the exit
    function still works and i can still compile
    program without any error messages using cc file.c
    if i use cc -Wall file.c i get error message
    file.c:18: warning: implicit declaration of function `exit'
    but program will still run and function properly
  • Jens.Toerring@physik.fu-berlin.de

    #2
    Re: exit()

    Darklight <nglennglen@net scape.net> wrote:[color=blue]
    > if the exit() function is contained in stdlid.h
    > why if i remove the above header file the exit
    > function still works and i can still compile
    > program without any error messages using cc file.c[/color]

    Without appropriate flags telling the compiler to output warnings
    many compilers only output messages for errors. Having no declaration
    in scope for a function isn't an error, the compiler will assume by
    default that that function is going to return an int (at least if you
    use a non-C99 compiler).
    [color=blue]
    > if i use cc -Wall file.c i get error message
    > file.c:18: warning: implicit declaration of function `exit'
    > but program will still run and function properly[/color]

    You're lucky. Even though the compiler had no information on what
    arguments exit() takes and it had to make a mistake in the assumption
    on what exit() returns (but, due to the nature of exit(), it's rather
    unimportant) it seems to have been able to produce a correct input
    file for the linker. And the linker doesn't care about declarations
    anymore, it just finds exit() in the C standard library of your
    system and makes the final executable use that.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ Jens.Toerring@p hysik.fu-berlin.de
    \______________ ____________ http://www.toerring.de

    Comment

    • Emmanuel Delahaye

      #3
      Re: exit()

      Darklight wrote on 03/09/04 :[color=blue]
      > if the exit() function is contained in stdlid.h[/color]

      It's not. A header only contains function declaration (in prototype
      forms since 1989). Please reread your C-book.
      [color=blue]
      > why if i remove the above header file the exit
      > function still works and i can still compile
      > program without any error messages using cc file.c
      > if i use cc -Wall file.c i get error message
      > file.c:18: warning: implicit declaration of function `exit'
      > but program will still run and function properly[/color]

      This is because the body of the function is elswere (probably in some
      library)

      --
      Emmanuel
      The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
      The C-library: http://www.dinkumware.com/refxc.html

      "C is a sharp tool"

      Comment

      • Flash Gordon

        #4
        Re: exit()

        On Fri, 3 Sep 2004 10:45:54 +0000 (UTC)
        Darklight <nglennglen@net scape.net> wrote:
        [color=blue]
        > if the exit() function is contained in stdlid.h
        > why if i remove the above header file the exit
        > function still works and i can still compile
        > program without any error messages using cc file.c
        > if i use cc -Wall file.c i get error message
        > file.c:18: warning: implicit declaration of function `exit'
        > but program will still run and function properly[/color]

        The exit function isn't contained in stdlib.h it is only declared there.
        The actual function is either compiler magic of contained in the
        standard library.

        The warning when using -Wall is the compiler being helpful.

        Please note that with some functions you invoke undefined behaviour if
        you don't have an appropriate declaration in scope and there are real
        systems where this can cause the software to fail. Even for the
        functions where it does not automatically invoke undefined behaviour it
        prevents the compiler from warning you if the parameters are incorrect
        (such as not passing any to exit) so you should always include the
        relevant header even if you can get away without it.

        Also, if using gcc (which -Wall suggests to me you might be) you might
        want to consider:
        gcc -ansi -pedantic -Wall -O file.c
        This will make gcc comply with the C89 (or possibly C95) standard and
        generate additional helpful warnings.
        --
        Flash Gordon
        Sometimes I think shooting would be far too good for some people.
        Although my email address says spam, it is real and I read it.

        Comment

        • Darklight

          #5
          Re: exit()

          Flash Gordon wrote:
          [color=blue]
          >
          > gcc -ansi -pedantic -Wall -O file.c[/color]

          Thanks for that

          Comment

          • CBFalconer

            #6
            Re: exit()

            Darklight wrote:[color=blue]
            > Flash Gordon wrote:
            >[color=green]
            >> gcc -ansi -pedantic -Wall -O file.c[/color]
            >
            > Thanks for that[/color]

            I use "gcc -W -Wall -ansi -pedantic -Wwrite-strings -O1"

            --
            "A man who is right every time is not likely to do very much."
            -- Francis Crick, co-discover of DNA
            "There is nothing more amazing than stupidity in action."
            -- Thomas Matthews


            Comment

            • SM Ryan

              #7
              Re: exit()

              Darklight <nglennglen@net scape.net> wrote:
              # if the exit() function is contained in stdlid.h
              # why if i remove the above header file the exit

              If you don't define or declare a function, it is implicitly declared as
              int functionname();
              (Returns int, takes an indefinite number of parameters converted with some
              default promotions.)

              If the function definition is compatiable with the implicit declaration,
              it will work.

              --
              SM Ryan http://www.rawbw.com/~wyrmwif/
              You hate people.
              But I love gatherings. Isn't it ironic.

              Comment

              • Dan Pop

                #8
                Re: exit()

                In <41389A21.7B314 626@yahoo.com> CBFalconer <cbfalconer@yah oo.com> writes:
                [color=blue]
                >Darklight wrote:[color=green]
                >> Flash Gordon wrote:
                >>[color=darkred]
                >>> gcc -ansi -pedantic -Wall -O file.c[/color]
                >>
                >> Thanks for that[/color]
                >
                >I use "gcc -W -Wall -ansi -pedantic -Wwrite-strings -O1"[/color]

                I don't. As the man page says, there are very good reasons -W and
                -Wwrite-strings have not been included into -Wall.

                If someone wants to bring gcc even closer to ANSI C conformance on x86,
                -ffloat-store should be added to the original set of options. But it
                slows down floating point code.

                Dan
                --
                Dan Pop
                DESY Zeuthen, RZ group
                Email: Dan.Pop@ifh.de

                Comment

                Working...