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)?
exit(int code);
is declared instdlib.h
so you need anAlso:
- You have no parameter for the
exit()
, it requires anint
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
is bad, too, it's:
The following example shows the usage of the
exit()
function.Output
The
exit()
function is a type of function with a return type without an argument. It's defined by the stdlib header file.You need to use
( exit(0) or exit(EXIT_SUCCESS))
or(exit(non-zero)
orexit(EXIT_FAILURE) )
.The
exit
function is declared in the stdlib header, so you need to haveat 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 likeexit()
, you have to call asexit(0)
orexit(42)
. 0 usually means your program completed successfully, and nonzero values are used as error codes.There are also predefined macros
EXIT_SUCCESS
andEXIT_FAILURE
, e.g.exit(EXIT_SUCCESS);
Write header file
#include<process.h>
and replaceexit();
withexit(0);
. This will definitely work in Turbo C; for other compilers I don't know.Try using
exit(0);
instead. Theexit
function expects an integer parameter. And don't forget to#include <stdlib.h>
.