有没有标准输出自动冲水退出前保证吗?它是如何工作的? [英] Is there a guarantee of stdout auto-flush before exit? How does it work?
问题描述
下面是code(有效的C和C ++)
Here is the code (valid C and C++)
#include <stdio.h>
int main() {
printf("asfd");
// LINE 1
return 0;
}
如果在1号线我把恩段错误pression程序只会崩溃而无任何打印(如预期)。
If in line 1 I put segfaulting expression the program would just crash without printing anything (as expected).
但为什么上述code印花ASDF和无缓冲退出不被刷新?什么是引擎盖下和为何如预期工作?
But why is the above code printing "asdf" and not exiting without buffer being flushed? What is under the hood and why does it work as expected?
推荐答案
这是由C ++语言规范这两个部分来完成:
This is accomplished by these two sections in the C++ language specification:
[basic.start.main]
在return语句主
有离开的主要功能和呼叫退出
的返回值的效果的说法。
A return statement in main
has the effect of leaving the main function and calling exit
with the return value as the argument.
和
[lib.support.start.term]
功能退出
在此国际标准的其他行为:
The function exit
has additional behavior in this International Standard:
- ...
- 接下来,所有打开C流与不成文的缓冲数据被刷新。
- ...
这篇关于有没有标准输出自动冲水退出前保证吗?它是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!