C Quirk??
Rich Salz
rsalz at bbn.com
Thu Feb 18 04:28:32 AEST 1988
=>>#define clearline() while (getchar() != '\n')
=> This is seriously bad code. What if stdin reaches end of file?
=
= It loops forever -- oops. Change that to
=#define clearline() while (1) {int ch = getchar(); \
= if ((ch == EOF) || (ch =='\n')) break; }
Not quite yet; the following "obvious" fragment won't compile
(try it before posting "why" to the net):
if (a)
if (b)
clearline();
else
printf("Don't know about b, but a is zero.\n");
This should work:
#define clearline() \
while (getchar() != '\n' && !feof(stdin) && !ferror(stdin))
As a general rule, you probably don't want to use { } in #define's
as { }; is not the same as { }.
/r$
--
For comp.sources.unix stuff, mail to sources at uunet.uu.net.
More information about the Comp.lang.c
mailing list