-3

why it showing different output???can anyone explain me in depth.

1.

#include <stdlib.h>
 #include <stdio.h>

int main (void)
{
printf ("Using exit ... \ n");
printf ("This is the content in buffer");
exit (0);
}

Output:

Using exit ... This is the content in buffer

2.

# Include <unistd.h>
# Include <stdio.h>

int main (void)
{
printf ("Using exit ... \ n");
printf ("This is the content in buffer");
_exit (0);
}

Only output:

Using exit ...

ajay154
  • 1
  • 3
  • maybe duplicate of http://stackoverflow.com/questions/5422831/what-is-the-difference-between-using-exit-exit-in-a-conventional-linux-fo or http://stackoverflow.com/questions/3657667/exit-functions-in-c – mpromonet Aug 09 '14 at 20:18
  • Possible duplicate of [Exit functions in C](http://stackoverflow.com/questions/3657667/exit-functions-in-c) – Thomas Dickey Mar 26 '17 at 00:21

2 Answers2

2

If we read _exit()'s documentation, we note:

Causes normal program termination to occur without completely cleaning the resources.

This presumably would include flushing stdout.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
0

First thing you should know is STDOUT is line buffered, means it flushes the memory after getting '\n'. Second thing is exit() flushes the stdio buffer while _exit() won't.

Now in your case, in first example exit() flushes the stdio buffer, So it is printing both printf output while in _exit() no flushing takes place so it is not printing both printf statement.

If you want to get correct output on the second statement, disable the buffering by putting

int main (void)
{
setbuf(stdout, NULL);
printf ("Using exit ... \ n");
printf ("This is the content in buffer");
_exit (0);
}
Sandeep_black
  • 1,352
  • 17
  • 18