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)?
Include
stdlib.h
in your header, and then callabort();
in any place you want to exit your program. Like this: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:
Use process.h instead of stdlib and iostream... It will work 100%.
You must add a line with
#include <stdlib.h>
to include that header file andexit
must return a value so assign some integer inexit(any_integer)
.In addition to return an exit code to parent process -
In UNIX, an important aspect that I think has been left out is, that exit() at first calls (in reverse order) all those functions, which were registered by atexit() call.
Please refer to SUSv4 for details.
on unix like operating systems exit belongs to group of system calls. system calls are special calls which enable user code (your code) to call kernel code. so exit call makes some OS specific clean-up actions before returning control to OS, it terminates the program.
Some compilers will give you the same opcode from both of these examples but some won't. For example opcode from first function will not include any kind of stack positioning opcode which will be included in the second example like for any other function. You could compile both examples and disassemble them and you will see the difference.
You can use exit from any part of your code and be sure that process terminates. Don't forget to include integer parameter.