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)?
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);
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.
C library function - exit() The C library function void exit(int status) terminates the calling process immediately.
Try using exit(0); instead. The exit function expects an integer parameter. And don't forget to #include <stdlib.h>.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With