考虑下一段代码:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
int printf(const char *format, ...)
{
va_list args;
return 1;
}
int main()
{
printf("Hello there");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
事实上,printf什么也没有打印。这意味着,编译器“更喜欢”这个函数而不是<stdio.h>. 但C不允许函数重写,根据我的理解,它应该抛出冲突错误的错误。
谁能解释这种行为?
C 标准规定(7.1.3 保留标识符):
因此,当<stdio.h>包含时,printf被保留为普通标识符的名称空间中具有文件范围的标识符。此外,即使不包含在内printf,也始终保留为具有外部链接的标识符。<stdio.h>
7.1.4 使用库函数不允许定义具有保留名称的函数。
在大多数实现中,此类定义将默默地替换库函数,但标准不保证任何内容。