我在/usr/include/stdio.h
在我可以访问的各种Linux系统上找到了这个有趣的代码。
167 /* Standard streams. */
168 extern struct _IO_FILE *stdin; /* Standard input stream. */
169 extern struct _IO_FILE *stdout; /* Standard output stream. */
170 extern struct _IO_FILE *stderr; /* Standard error output stream. */
171 /* C89/C99 say they're macros. Make them happy. */
172 #define stdin stdin
173 #define stdout stdout
174 #define stderr stderr
问题:定义一个基本上什么都没有的宏有什么意义?
答案 0 :(得分:2)
C11在7.21.1p3中定义stdin
,stdout
和stderr
:
- 醇>
[{1}}]中定义的宏
[...]
<stdio.h>
stderr
stdin
是指向
stdout
的类型的表达式,它们分别指向与标准错误,输入和输出流关联的FILE
对象。[...]
由于标准说这些是宏,因此它们必须是宏。但是,表达式必须有一些定义,标准允许它们也用作变量名,但它们不必是实际的标识符。
Linux userland除了符合C11标准外,还尝试大多符合POSIX标准。在POSIX中,要求是these symbols are external identifiers。由于C11要求它们是宏,并且POSIX要求它们是外部标识符,所以通过使用与相应标识符完全相同的名称定义宏来实现这一点。
如果POSIX标准打算 它们也是具有外部链接的变量,那确实值得商榷,但值得注意的是POSIX.1 2008 standard在其中确实存在状态
系统接口卷与合规性相关的唯一部分是“概要”,“描述”,“返回值”和“错误”部分。
这些SYNOPSIS section确实说:
<强>概要强>
FILE