Is getc a macro or a function?
我试图找出
The difference between getc and fgetc is that getc can be implemented as a macro, whereas fgetc cannot be implemented as a macro.
那么,
The difference between getc and fgetc is that getc can be implemented as a macro, whereas fgetc cannot be implemented as a macro.
那是不正确的。
除了少数例外,任何 C 标准库函数都可以另外实现为宏,但该宏的编写方式存在一些限制(因此宏仍可以像函数一样使用)。
引用 N1570 第 7.1.4 节:
Any function declared in a header may be additionally implemented as a
function-like macro defined in the header, [...] Any invocation of a
library function that is implemented as a macro shall expand to code
that evaluates each of its arguments exactly once, fully protected by
parentheses where necessary, so it is generally safe to use arbitrary
expressions as arguments.
The
getc function is equivalent tofgetc , except that if
it is implemented as a macro, it may evaluatestream more than
once, so the argument should never be an expression with side effects.
在
So, is
getc implemented in C or not?
也许吧。任何 C 库函数都不需要在 C 中实现。某些函数可以用汇编程序或任何其他语言实现 - 或者它们可能是编译器内在函数。通常大多数或所有 C 标准库函数都是用 C 实现的,但标准只指定它们的工作方式,而不是它们的编写方式。
The
getc function is equivalent tofgetc , except that if it is implemented as a macro, it may evaluatestream more than once, so the argument should never be an expression with side effects.
它不适用于具有副作用的参数。例如 --
1 |