在阅读 getc
手册页时,我遇到了:
may be implemented as a macro which evaluates stream more than once.
那么,getc
也可以作为一个函数来实现——多次评估流究竟意味着什么?
最佳答案
考虑一个函数:
FILE *getstream(void);
还有一个代码:
int c = getc(getstream());
如果 getc
是一个函数,getstream
将只被调用一次,其结果将存储在一个临时变量/寄存器中并传递给 getc
.
如果 getc
是一个宏,可以这样说:
#define getc(s) ((s) == NULL) ? -1 :_getc(s))
它将扩展为:
int c = (((getstream()) == NULL) ? -1 :_getc(getstream()));
也就是说,它将调用 getstream()
两次。万一 getstream
有一些副作用(使用 IO,修改一些全局状态),程序的流程在这两种情况下会有所不同,必须意识到这一点。
https://stackoverflow.com/questions/69315769/
相关文章:
python - 对具有更高优先级正数的绝对值的整数列表进行排序
node.js - 超时错误 - MongoServerSelectionError:连接 ECON
php - laravel sanctum 路线 [登录名] 未定义
haskell - 我可以说 Monad 使将某些类型视为同构成为可能吗?
android - 我正在尝试使用安全参数,但是当我将依赖项添加到项目级别 build.gradle
ios - 通过 FaSTLane "the server responded with statu