这个问题正在寻找明确解释为什么这种行为是错误的标准语录。
下面的代码包括<stdio.h>
里面main
,
int main()
{
#include <stdio.h>
printf("hello , world \n");
return 0;
}
关于 gcc -Wall in.c -o in.out
它成功编译并打印hello , world
.
但是在 clang in.c -o in.out
它给了我这个错误:
/usr/include/stdio.h:353:12: error: implicit declaration of 'fprintf' requires
inclusion of the header <stdio.h>
extern int fprintf (FILE *__restrict __stream,
^
1 error generated.
我怀疑这是什么行为?这是未定义的行为还是什么?
我也找不到与之相关的文档。
编辑:问题是我在某个类似的地方找到了这段代码,但我无法准确发布该代码,所以我发布了这种演示代码。我知道配售 stdio.h
在主体之外。
最佳答案
C99,7.1.2/4:
[...] If used, a header shall be included outside of any external declaration or definition, and it shall first be included before the first reference to any of the functions or objects it declares, or to any of the types or macros it defines.
4/2:
If a ‘‘shall’’ or ‘‘shall not’’ requirement that appears outside of a constraint is violated, the behavior is undefined.
6.9/4:
As discussed in 5.1.1.1, the unit of program text after preprocessing is a translation unit, which consists of a sequence of external declarations. These are described as ‘‘external’’ because they appear outside any function (and hence have file scope).
所以我认为这是未定义的行为。
关于c++ - 这是什么类型的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13678028/