whitespace in the format string (scanf)

Whitespace in the format string matches 0 or more whitespace characters in the input.

So "%d c %d" expects number, then any amount of whitespace characters, then character c, then any amount of whitespace characters and another number at the end.

"%dc%d" expects number, c, number.


Also note, that if you use * in the format string, it suppresses assignment:
%*c = read 1 character, but don’t assign it to any variable

So you can use "%d%*c c%*c %d" if you want to force user to enter: number, at least 1 character followed by any amount of whitespace characters, c, at least 1 character followed by any amount of whitespace characters again and number.

Leave a Comment