CBFalconer wrote:
>
pete wrote:
In cases when the getc macro is supplied, then the getc function
most likely is nothing more than a wrapper for either the getc
macro or an equivalent macro.
Not so. Note the provisions for multiple evaluation of the getc
parameter. Thus the macro may not be usable for some calls,
requiring the use of:
whatever = (getc)(f);
which avoids calling the macro.
You're missing a point.
The multiple evaluation of an argument by a macro,
only makes a difference when the evaluation of
the argument has side effects.
Concerning the getc function,
the evaluation of the argument in a function call
will have side effects,
but the evaluation of the parameter object inside
of the function will not have side effects,
it's just a simple pointer object.
If the wrapper function then calls the macro,
then it won't matter if the macro evaluates
the argument 5 zillion times,
the side effects will only occur once.
The point is simpler to illustrate with putc than getc:
/* BEGIN new.c output */
The putc macro output, with 'X' as the int argument
when the stream argument outputs '0' as a side effect:00X
The putc_wrapper output, with 'X' as the int argument
when the stream argument outputs '0' as a side effect:0X
/* BEGIN new.c output */
/* BEGIN new.c */
#include<stdio.h>
int putc_wrapper(int c, FILE *stream)
{
return putc(c, stream);
}
int main(void)
{
#ifndef putc
puts("putc isn't a macro here and now.");
#else
puts("/* BEGIN new.c output */\n");
printf("The putc macro output, with 'X' as the int argument\n"
"when the stream argument outputs '0' as a side effect:");
putc ('X', (printf("0"), stdout));
puts("\n");
printf("The putc_wrapper output, with 'X' as the int argument\n"
"when the stream argument outputs '0' as a side effect:");
putc_wrapper('X', (printf("0"), stdout));
puts("\n\n/* BEGIN new.c output */");
#endif
return 0;
}
/* END new.c */
--
pete