Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What int values are relevant for exit() in C?

Tags:

c

exit

On TutorialsPoint.com, exit is passed the value 0, while people often pass it 1. I've even seen exit(3);

What do the different values mean?

like image 465
jellies Avatar asked Apr 28 '16 02:04

jellies


People also ask

What is exit () function in C?

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. The general form of the exit() function is as follows − void exit (int code);

What is the use of exit 0 in C?

exit is a jump statement in C/C++ language which takes an integer (zero or non zero) to represent different exit status. Exit Success: Exit Success is indicated by exit(0) statement which means successful termination of the program, i.e. program has been executed without any error or interrupt.

Is return 0 the same as exit 0?

In C++, what is the difference between exit(0) and return 0 ? When exit(0) is used to exit from program, destructors for locally scoped non-static objects are not called. But destructors are called if return 0 is used.

What is the difference between exit () and return () in C?

return returns from the current function; it's a language keyword like for or break . exit() terminates the whole program, wherever you call it from.


1 Answers

By convention, a program that exits successfully calls exit (or returns from main) with a value of 0. Shell programs (most programs, actually) will look for this to determine if a program ran successfully or not.

Any other value is considered an abnormal exit. What each of those values mean is defined by the program in question.

On Unix and similar systems, only the lower 8 bits of the exit value are used as the exit code of the program and are returned to the parent process on a call to wait. Calling exit(n) is equivalent to calling exit(n & 0xff)

From the man page:

The exit() function causes normal process termination and the value of status & 0377 is returned to the parent (see wait(2)).

like image 71
dbush Avatar answered Sep 29 '22 08:09

dbush