Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of exit() function

Tags:

c

I want to know how and when can I use the exit() function like the program in my 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"); } 

When I run it, it shows ERROR: call to undefined function exit().

Also, I want to know how I can create an option to close the window in which the program runs? For example, I made a menu-driven program which had several options and one of them was "exit the menu". How can I make this exit the program (i.e. close the window)?

like image 258
Kraken Avatar asked Mar 11 '10 13:03

Kraken


People also ask

What is the use of exit () function in C?

What is exit() function in C language? The exit () function is used to break out of a loop. This function causes an immediate termination of the entire program done by the operation system. void exit (int code);

Why do you need exit () function?

Theoretically, the exit() function in C++ causes the respective program to terminate as soon as the function is encountered, no matter where it appears in the program listing. The function has been defined under the stdlib. h header file, which must be included while using the exit() function.

What library is the exit () function in?

C library function - exit() The C library function void exit(int status) terminates the calling process immediately.


2 Answers

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

like image 198
Klaus Byskov Pedersen Avatar answered Sep 28 '22 12:09

Klaus Byskov Pedersen


The exit function is declared 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);

like image 32
Tyler McHenry Avatar answered Sep 28 '22 12:09

Tyler McHenry