No. The exit() function is used to exit your program and return control to the operating system. The return statement is used to return from a function and return control to the calling function. If you issue a return from the main() function, you are essentially returning control to the calling function, which is the operating system. In this case, the return statement and exit() function are similar.
Prototype of atexit() is : int atexit ( void ( * function ) (void) );
The function pointed by the function pointer argument is called when the program terminates normally.
If more than one atexit function has been specified by different calls to this function, they are all executed in reverse order as a stack, i.e. the last function specified is the first to be executed at exit.
One single function can be registered to be executed at exit more than once.
/* atexit example */ #include <stdio.h> #include <stdlib.h>
void fnExit1 (void) { puts ("Exit function 1."); }
void fnExit2 (void) { puts ("Exit function 2."); }
int main () { atexit (fnExit1); atexit (fnExit2); puts ("Main function."); return 0; }
Output:
Main function. Exit function 2. Exit function 1.
//Here the output is in reverse order as a stack, i.e. the last function specified is the first to be executed at exit.
Exit and return serves differnt purposes. Exit is used to terminate the calling program while return is used to return the value to calling program eg int sum(int a, int b); main() int a,b,c; printf("Enter the value of a and b); scanf("%d %d",&a,&b); c=sum(a,b); exit(0); printf("Sum is %d",c); } int sum(int a, int b) { int d; d=a+b; return(d); } in this bcause of exit function the output will not be displayed but if we delete line exit(0) then we will get output