scanf/discarding remaining whitespace

Ron Stanonik stanonik at nprdc.navy.mil
Fri Oct 19 03:10:01 AEST 1990


A thought about scanf:

The scanf suppression character * means "suppress one"; ie, there
had better be one to suppress, otherwise the control string isn't
matched and scanning stops.  But if it meant "suppress zero or one",
then discarding trailing whitespace (including the newline) could
be accomplished with

	scanf("%d%*[^\n]%*c", &i);

It turns out this works in 4.3bsd, though I haven't dug into scanf
yet to find out why; ie, because of a bug or because * is interpreted
as "zero or one".

Ron Stanonik
stanonik at nprdc.navy.mil



More information about the Comp.unix.questions mailing list