I was reading the C programming book by Kernighan and and came across putc
and fputc
. I didn't quite understand the difference between the two or when would one be used over the other. I found some posts on StackOverflow which dealt with this topic but it's still unclear.
As mentioned here (putc needs stdout, vs puts) :
According to Kernighan's book
putc
is equivalent tofputc
butputc
could be implemented as a macro andputc
may evaluate its stream argument more than once.The difference between
putc
andfputc
is that by usingputc
, you risk running the macro version which is inherently unsafe because it may have to evaluate its stream argument more than once. This causes complications that most people aren't aware of and thus do not watch out for, sofputc
is better to use.fputc
's macro does not have this problem.
Questions:
putc
can be implemented as a macro but what is the problem doing same withfputc
?The second statement mentions of some complications and safety issues. What are those?
putc
evaluates its argument more than once. So what are the advantages or disadvantages it poses compared to evaluating the argument.