Thread: Getting a Warning When I use exit()

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    127

    Getting a Warning When I use exit()

    Hi,

    In an example C program, I saw someone use the following statement:

    Code:
    exit();
    If I use this in a program which I compile in Visual Studio Express 2008, I get the following warning:

    "warning C4013: 'exit' undefined; assuming extern returning int"

    Does anyone know what's going on here? I've noticed that if I add #include <stdlib.h>, I no longer get the same warning. Instead, it gives me an error, and tells me I need to give it a value. So what would the correct usage of it be? Is it worth including <stdlib.h>?

    Thanks.

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    There should be an argument with exit, like this exit(0), exit(1), etc.....
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Thats because exit() is defined in stdlib.h and before you added it the compiler didnt know what it was, and after you did add it, like BEN10 says, you didn't pass in the value which it expects.
    Spidey out!

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Here is what the standard has to say about exit.
    Originally Posted by C Standard, 1999 Edition, Section 7.20.4.3
    Code:
    #include <stdlib.h>
    void exit(int status);The exit function causes normal program termination to occur. If more than one call to the exit function is executed by a program, the behavior is undefined.

    First, all functions registered by the atexit function are called, in the reverse order of their registration, except that a function is called after any previously registered functions that had already been called at the time it was registered. If, during the call to any such function, a call to the longjmp function is made that would terminate the call to the registered function, the behavior is undefined.

    Next, all open streams with unwritten buffered data are flushed, all open streams are closed, and all files created by the tmpfile function are removed.

    Finally, control is returned to the host environment. If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. If the value of status is EXIT_FAILURE, an implementation-defined form of the status unsuccessful termination is returned. Otherwise the status returned is implementation-defined.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    127
    OK, thanks. Maybe that seemed like a dumb question, but upto recently, I've only been programming in C++. From what I've done with that, it seems like if you call a function and it hasn't been declared somewhere, it just won't compile.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    No. That's true no matter what language. Everything must be defined... Perhaps the compile phase may blow up at various times - such as during linking if the syntax seemed alright throughout.

    In the above case an undefined function was caught by inference: there was no function prototype found so default assumptions were made as to its form. And that was only a WARNING. A more severe error would have shown up later as well indicating a missing function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 02:06 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM