different output with exit() and _exit()?

﹥>﹥吖頭↗ 提交于 2019-12-14 03:37:06

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!