in C by (9.9k points)
0 like 0 dislike

What is difference between return 1 and return 0 exit code in C and C++ programing language? When should I return 1? Why main function return 0 for success in C? What exit code number mean success and what failure?

main return 0 or 1

1 Answer

0 like 0 dislike
by (9.9k points)

It's defined by C standard as 0 for success and 1 for fail. This value returned to OS from main, exit or similar calls.

The reason why main use 0 for success is that it is used as the exit code of the application to the operating system, where 0 typically means success and 1 (or higher) means failure. (Of course, you should always use the predefined macros EXIT_SUCCESS and EXIT_FAILURE.)

Inside an application, however, it's more natural to use zero for failure and non-zero for success, as the return value can directly be used in an if condition.

But for greater portability, you can use macros EXIT_SUCCESS and EXIT_FAILURE for conventional status value for success and failure, respectively. They are declared in file "stdlib.h".

Your answer

Try to answer the question as detailed as possible.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.