Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: short code sample for returning from main

...

Since main() is defined to have return type int, another valid exit strategy is to simply use a return statement.

Code Block
bgColor#ccccff

int main(int argc, char **argv) {
  /* ... */
  return status;
}

Wiki Markup
\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] (C99) section 5.1.2.2.3 has this to say about returning from {{main()}}:

...

The quickest and most abrupt way to terminate a program, abort() takes no arguments, and always signifies abnormal termination to the operating system.

Code Block
bgColor#ccccff

{code:bgColor=#ccccff}
#include <stdlib.h>
/* ... */

if (/* something really bad happened */) {
  abort();
}

...