In the following if I write scanf with %d /%d" (with a space) then I can input a fraction seperated by spaces.
For example: The input can be 5/7 or 5 /7 or 5 / 7 (with spaces anywhere).
How does scanf in the code below read spaces between numbers that way but yet it is has only one space between %d /%d"?
#include <stdio.h>
int main()
{
int i, j;
scanf("%d /%d", &i, &j);
return 0;
}
Almost all input format specifiers in
scanfwill automatically skip all whitespace before reading the actual data. E.g. the%dformat specifier inscanfactually means<skip spaces> <read int>.That means that if we used
%d/%dformat for reading fractions, it would stand forIn this sequence we have everything necessary, except we don’t allow/skip spaces before the
/. To fix that we have to add an explicit request to skip spaces before/, which inscanfformat string is expressed by a space character. Adding a space character before/produces%d /%dformat string, which stands forThis is exactly what we need and that is exactly what you have in your code.