"C89/C99 say they're macros. Make them happy."?
"C89/C99 say they're macros. Make them happy."?
我在我可以访问的各种 Linux 系统上的 /usr/include/stdio.h
中发现了这个有趣的代码。
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
问题:定义一个基本上什么都不做的宏有什么意义?
C11在7.21.1p3中定义了stdin
、stdout
和stderr
:
The macros [defined in <stdio.h>
] are
[...]
stderr
stdin
stdout
which are expressions of type pointer to FILE
that point to the FILE
objects associated, respectively, with the standard error, input, and output streams.
[...]
既然标准说这些是宏,那它们就一定是宏。然而,表达式必须有一些定义,并且标准 允许 它们 也 被用作变量名,但它们不必是实际的标识符。
Linux userland 除了符合 C11 标准外,还试图在很大程度上符合 POSIX 标准。在POSIX中,要求是these symbols are external identifiers。由于 C11 要求它们是宏并且 POSIX 要求它们是外部标识符,因此通过使用与相应标识符完全相同的名称定义宏来实现这一点最简单。
POSIX标准是否打算它们也是具有外部链接的变量确实值得商榷,但它是值得注意的是 POSIX.1 2008 standard 在其系统接口卷中确实声明
The only sections relating to conformance are the SYNOPSIS, DESCRIPTION, RETURN VALUE, and ERRORS sections.
而这些 SYNOPSIS section 确实是这样说的:
SYNOPSIS
#include <stdio.h>
extern FILE *stderr, *stdin, *stdout;
我在我可以访问的各种 Linux 系统上的 /usr/include/stdio.h
中发现了这个有趣的代码。
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
C11在7.21.1p3中定义了stdin
、stdout
和stderr
:
The macros [defined in
<stdio.h>
] are[...]
stderr
stdin
stdout
which are expressions of type pointer to
FILE
that point to theFILE
objects associated, respectively, with the standard error, input, and output streams.[...]
既然标准说这些是宏,那它们就一定是宏。然而,表达式必须有一些定义,并且标准 允许 它们 也 被用作变量名,但它们不必是实际的标识符。
Linux userland 除了符合 C11 标准外,还试图在很大程度上符合 POSIX 标准。在POSIX中,要求是these symbols are external identifiers。由于 C11 要求它们是宏并且 POSIX 要求它们是外部标识符,因此通过使用与相应标识符完全相同的名称定义宏来实现这一点最简单。
POSIX标准是否打算它们也是具有外部链接的变量确实值得商榷,但它是值得注意的是 POSIX.1 2008 standard 在其系统接口卷中确实声明
The only sections relating to conformance are the SYNOPSIS, DESCRIPTION, RETURN VALUE, and ERRORS sections.
而这些 SYNOPSIS section 确实是这样说的:
SYNOPSIS
#include <stdio.h> extern FILE *stderr, *stdin, *stdout;