问题
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 ...
回答1:
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.
回答2:
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);
}
来源:https://stackoverflow.com/questions/25222617/different-output-with-exit-and-exit