getc() vs fgetc() - What are the major differences?

前端 未结 3 643
慢半拍i
慢半拍i 2020-12-23 13:33

Everywhere I see \"it is practically identical\", or something similar...

From The GNU C Programming Tutorial :

There is another function in t

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-23 14:22

    Seems like the differences are, in 99.9% of the cases, meaningless.

    One point which may make a difference - The man page says getc() may be implemented as a macro which evaluates stream more than once.

    It could lead to strange behavior in some (not very useful) cases, e.g.:

    FILE *my_files[10] = {...}, *f=&my_files[0];
    for (i=0; i<10; i++) {
        int c = getc(f++);    // Parameter to getc has side effects!
    }
    

    If getc evaluates f++ more than once, it will advance f more than once per iteration. In comparison, fgetc is safe in such situations.

提交回复
热议问题