Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Consequently, returning from main() is equivalent to calling exit(). Many compilers implement this behavior with something analogous to

Code Block
void _start(void) {
  /* ... */
  exit(main(argc, argv));
}

...

Code Block
bgColor#FFCCCC
#include <stdlib.h>
#include <stdio.h>

int write_data(void) {
  char const *filename = "hello.txt";
  FILE *f = fopen(filename, "w");
  if (f == NULL) {
    /* handle error */
  }
  fprintf(f, "Hello, World\n");
  /* ... */
  abort(); /* oops! data might not get written! */
  /* ... */
  return 0;
}

int main(void) {
  write_data();
  return 0;
}

...

Code Block
bgColor#ccccff
#include <stdlib.h>
#include <stdio.h>

int write_data(void) {
  char const *filename = "hello.txt";
  FILE *f = fopen(filename, "w");
  if (f == NULL) {
    /* handle error */
  }
  fprintf(f, "Hello, World\n");
  /* ... */
  exit(EXIT_FAILURE); /* writes data & closes f. */
  /* ... */
  return 0;
}

int main(void) {
  write_data();
  return 0;
}

...