What should main() return in C and C++?

  • The return value for main is used to indicate how the program exited. If the program execution was normal, a 0 return value is used. Abnormal termination(errors, invalid inputs, segmentation faults, etc.) is usually terminated by a non-zero return. There is no standard for how non-zero codes are interpreted.
  • You can define your own status codes though and use them to represent different types of failures in the program.
  • GCC recommends using either EXIT_SUCCESS or EXIT_FAILURE defined in stdlib.h for sending exit signals.
  • In C++, int main() can be left without a return value at which point it defaults to returning 0.