Search code examples
cprintfcallexitdisassembly

Call to printf doesn't show up in disassembly after a call to exit function


#include <stdio.h>
#include <stdlib.h>
int main()
{
     exit(0);
     printf("%s\n", "Nice");
}

I was wondering if it is possible to disable whatever it is that doesn't generate the instruction for a call to printf if it is placed right after a call to exit. I'm using gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.4) and no flags when compiling. I have tried using -O0, but there is no change in the disassembly.

   0x000000000040052d <+0>:     push   %rbp
   0x000000000040052e <+1>:     mov    %rsp,%rbp
   0x0000000000400531 <+4>:     mov    $0x0,%edi
   0x0000000000400536 <+9>:     callq  0x400430 <exit@plt>

Solution

  • exit() is defined in <stdlib.h> as never returning to its caller:

    _Noreturn void exit(int status);
    

    The compiler takes advantage of this and does not generate code for statements that are never reached. If you pass -Wall to get more diagnostics, you will get a warning about this.