c++ - 这是什么类型的行为?

标签 c++ c gcc clang

这个问题正在寻找明确解释为什么这种行为是错误的标准语录。

下面的代码包括<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/

相关文章:

c++ - 根据条件替换指令

c++ - 字符串实现的排列问题

c++ - Int vs Double 汇编

c++ - GCC 在要求此引用声明的 constexpr 说明符方面是否正确?

c++ - GCC 警告将函数指针转换为对象指针

c++ - OpenGL Programming Guild 第八版,示例程序和 'NumVAOs'

c++ - 在 fscanf 中直接将 char* 转换为 std::string

c - FTP 客户端无法与我实现的 TCP 服务器通信

c - 数组初始化中数组元素的赋值

c - 在 C 中定义 #if 预处理器条件