"Advanced Programming in the UNIX Environment, 3rd Edition", page 151:
The difference between
getcandfgetcis thatgetccan be implemented as a macro, whereasfgetccannot be implemented as a macro. This means three things:
- The argument to
getcshould not be an expression with side effects.- Since
fgetcis guaranteed to be a function, we can take its address. This allows us to pass the address offgetcas an argument to another function.- Calls to
fgetcprobably take longer than calls togetc, as it usually takes more time to call a function.
What "expression with side effects" can occur for the function signatures with stream pointer as a parameter?
#include<stdio.h>
int getc(FILE* stream);
int fgetc(FILE* stream);
There are probably hundreds of ways to pass an expression with side effects, but a "credible" one would be something like:
If
getcis implemented poorly as a macro, the expressionfiles[counter++]could be evaluated more than once, leading to unexpected behavior.