gpt4 book ai didi

c - getc() vs fgetc() - 主要区别是什么?

转载 作者:太空狗 更新时间:2023-10-29 16:19:29 28 4
gpt4 key购买 nike

我到处都看到“它实际上是相同的”或类似的东西......

来自 The GNU C Programming Tutorial :

There is another function in the GNU C Library called fgetc. It is identical to getc in most respects, except that getc is usually implemented as a macro function and is highly optimised, so is preferable in most situations. (In situations where you are reading from standard input, getc is about as fast as fgetc, since humans type slowly compared to how fast computers can read their input, but when you are reading from a stream that is not interactively produced by a human, fgetc is probably better.)

还有哪些区别?我听说它们每个都有不同的实现(并且一个可以用作宏)但是,是什么让它们如此不同(或足够不同)以至于它们都在标准 C 库(或规范)中?

最佳答案

来自 Unix 环境高级编程:

...

The difference between getc and fgetc is that getc can be implemented as a macro, whereas fgetc cannot be implemented as a macro. This means three things:

  • The argument to getc should not be an expression with side effects.
  • Since fgetc is guaranteed to be a function, we can take its address. This allows us to pass the address of fgetc as an argument to another function.
  • Calls to fgetc probably take longer than calls to getc, as it usually takes more time to call a function.

...

关于c - getc() vs fgetc() - 主要区别是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18480982/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com