title: Difference between exit() and return in main() function in C
body: <p>I've looked through the links <a href="https://stackoverflow.com/questions/3463551/difference-between-exit-and-return">What is the difference between exit and return?</a>
  and
  <a href="https://stackoverflow.com/questions/461449/return-statement-vs-exit-in-main">return statement vs exit() in main()</a>
  to find the answer, but in vain.</p>
  
  <p>Problem with the first link is that the answer assumes <code>return</code> from any function. I want to know the exact difference between the two when in main() function. Even if there's a little difference I'd like to know what it is. Which is preferred and why? Is there any performance gain in using <code>return</code> over exit() (or exit() over <code>return</code>) with all sorts of compiler optimizations turned off?</p>
  
  <p>Problem with the second link is I'm not interested in knowing what happens in C++. I want the answer specifically pertaining to C.</p>
  
  <p>EDIT:
  After recommendation by a person, I actually tried to compare the assembly output of the following programs:</p>
  
  <p>Note: Using <code>gcc -S &lt;myprogram&gt;.c</code></p>
  
  <p>Program mainf.c:</p>
  
  <pre><code>int main(void){
   return 0;
  }
  </code></pre>
  
  <p>Assembly output:</p>
  
  <pre><code>    .file   "mainf.c"
      .text
      .globl  main
      .type   main, @function
  main:
  .LFB0:
      .cfi_startproc
      pushq   %rbp
      .cfi_def_cfa_offset 16
      .cfi_offset 6, -16
      movq    %rsp, %rbp
      .cfi_def_cfa_register 6
      movl    $0, %eax
      popq    %rbp
      .cfi_def_cfa 7, 8
      ret
      .cfi_endproc
  .LFE0:
      .size   main, .-main
      .ident  "GCC: (Ubuntu 4.9.2-10ubuntu13) 4.9.2"
      .section    .note.GNU-stack,"",@progbits
  </code></pre>
  
  <p>Program mainf1.c:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  
  int main(void){
   exit(0);
  }
  </code></pre>
  
  <p>Assembly output:</p>
  
  <pre><code>    .file   "mainf1.c"
      .text
      .globl  main
      .type   main, @function
  main:
  .LFB2:
      .cfi_startproc
      pushq   %rbp
      .cfi_def_cfa_offset 16
      .cfi_offset 6, -16
      movq    %rsp, %rbp
      .cfi_def_cfa_register 6
      movl    $0, %edi
      call    exit
      .cfi_endproc
  .LFE2:
      .size   main, .-main
      .ident  "GCC: (Ubuntu 4.9.2-10ubuntu13) 4.9.2"
      .section    .note.GNU-stack,"",@progbits
  </code></pre>
  
  <p>Noting that I'm not well versed with assembly, I can see some differences between the 2 programs with the <code>exit()</code> version being shorter than <code>return</code> version. What's the difference?</p>