what is the difference in exit(0),exit(1),exit(2) ?
how it differs from Return statement?

Recommended Answers

All 6 Replies

The argument to the "exit()" function is passed back to the calling process by the operating system. If you run the program from a command line, a command line script may be able use the return value to cause it to do something, or it may simply ignore the return value. If the calling program is another program (through a system() call, for example), the return value may mean something significant to allow the calling program take action dependent on the returned value.

The actual mechanism for returning a value is not part of the C language (it's operating-system dependent). The only two values defined in Standard C are the EXIT_SUCCESS, or EXIT_FAILURE macros in <stdlib.h>. That is, "exit(3)", for example, may cause different actions, depending on the operating system.

The statement "exit(EXIT_SUCCESS)" is equivalent to exit(0). The result of an "exit(0)" is the same as a "return 0;" at the end of main().

Typically people use "return(0)" or "exit(0)" or "exit(EXIT_SUCCESS)" unless they want to give the calling program to discriminate between a satisfactory termination and termination caused by an error condition.

You normally use exit(0) if everything went ok. But if your program detects an error and decides to abort, you would use exit(1). Or you might use exit(1), exit(2), etc, with each exit code meaning some specific error.
Read more about exit code - http://www.chicagotech.net/troubleshooting/exitcode0.htm

Or don't use exit() at all, unless there's some sort of critical lockup that keeps your program from going any further.

Generally good error handling will lead you to the end of main, with just a return/break or two and with virtually no overhead.

exit ,to terminate excutation of the programe and return is the out of the programe.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.