tags:

views:

174

answers:

4

i want to know how and when can i use exit() function..like i had a programme written in book..

#include<stdio.h>

void main()
{
    int goals;
    printf("enter number of goals scored");
    scanf("%d",&goals);

    if(goals<=5)
        goto sos;
    else
    {
        printf("hehe");
        exit( );
    }
    sos:
    printf("to err is human");
}

But when I run it, it shows error that call to undefined function exit().. Since I'm a beginner in c language so kindly help me out.. Also I want to know that if within a programme I want to close the window in which the whole programme runs..how can i do that.for e.g. I made a menu driven programme which had several option and one of them was "exit the menu"...And when I opt for exit option the programme shall stop working..i.e. the window shall close... kindly help..if u get what I'm saying.. :(

+2  A: 

Try using exit(0); instead. The exit function expects an integer parameter. and don't forget to #include <stdlib.h>.

klausbyskov
thnx..for the answer..
hopefulLLl
+4  A: 

The exit function is defined in the stdlib header, so you need to have

#include <stdlib.h>

at the top of your program to be able to use exit.

Note also that exit takes an integer argument, so you can't call it like exit(), you have to call as exit(0) or exit(42). 0 usually means your program completed successfully, and nonzero values are used as error codes.

There are also predefined macros EXIT_SUCCESS and EXIT_FAILURE, e.g. exit(EXIT_SUCCESS);

Tyler McHenry
+1  A: 

exit(int code); is defined in stdlib.h so you need an

#include <stdlib.h>

Also:
- You have no parameter for the exit(), it requires an int so provide one.
- Burn this book, it uses goto which is (for everyone but linux kernel hackers) bad, very, very, VERY bad.

Edit:
Oh, and

void main()

is bad, too, it's:

int main(int argc, char *argv[])
dbemerlin
yeah its written in the book that u better sont use goto bt for the sake of completeness of the book m just giving u an example...so the book aint that bad!!
hopefulLLl
thnx for the answer..
hopefulLLl
+1  A: 

Did you try man exit ?


Oh, and what about

#include <stdlib.h>

int main(void) {
  /*  ...  */
  if (error_occured) {
    return (EXIT_FAILURE);
  }
  /*  ...  */
  return (EXIT_SUCCESS);
}

?

Bertrand Marron