I want to know how and when can I use the exit()
function like the program in my book:
#include
void main()
{
int goals;
Try using exit(0);
instead. The exit function expects an integer parameter. And don't forget to #include <stdlib.h>
.
You must add a line with #include <stdlib.h>
to include that header file
and exit
must return a value so assign some integer in exit(any_integer)
.
exit(int code);
is declared 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[])
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)
or exit(EXIT_FAILURE) )
.
Include stdlib.h
in your header, and then call abort();
in any place you want to exit your program. Like this:
switch(varName)
{
case 1:
blah blah;
case 2:
blah blah;
case 3:
abort();
}
When the user enters the switch accepts this and give it to the case 3 where you call the abort
function. It will exit your screen immediately after hitting enter key.
Try man exit.
Oh, and:
#include <stdlib.h>
int main(void) {
/* ... */
if (error_occured) {
return (EXIT_FAILURE);
}
/* ... */
return (EXIT_SUCCESS);
}