Q: Why use white-space around "%*c"
?
A: The white-space in " %*c"
is a scanf()
directive that skips white-spaces (0 or more) during the scan thus preventing "%*c"
from itself reading a white-space. Without the leading white-space, "%*c"
will scan any 1 char
, white-space or not. The trailing white-space in "%*c "
has no effect at all on what "%*c"
already did. The trailing white-space directive simple skips subsequent white-spaces.
White-space in a format is not needed around "%*c"
, it depends on the coding goal.
Most specifiers, like "%d"
, "%f"
, "%s"
skip leading white-spaces. (White-space being ' '
, '\n'
, '\t'
, etc. see isspace()
).
Simply having a format directive " "
or "\n"
also skips white-spaces.
3 specifiers "%c"
, "%n"
and "%[scanset]"
do not skip leading white-spaces.
Since many programmers want scanf()
to skip leading white-spaces before a "%c"
a preceding white-space is needed as in " %c"
.
Note: "%d %*c %d"
will scan the same as "%d %*c%d"
.