I'm trying to figure out exactly how scanf
works. If I code something like this:
scanf("%s %d %f", name, &age, &wage);
enter this as standard input:
james 20 34000.25
and then print out name, age, and wage respectively, I get exactly what one would expect, the same thing as I put into standard input. However, if I enter something like this:
scanf("%s/%d/%f", name, &age, &wage);
and I enter this as standard input:
james/20/34000.25
I get the string james/20/34000.25
for the string value and 0 and 0.00000 respectively for the integer and float values. I thought scanf
would treat the slashes the same as it would treat the spaces in the first version. How would I get it so if a user enters a slash separated value, I can appropriately assign the values to variables?
%s
,%d
,%f
,%[^/]
or `%[^ /] format specifications were successfully matched? What happens if the user simply presses CTRL+Z in Windows, or CTRL+d in Linux to close stdin? scanf returns and your program is happy to use garbage values? What does scanf return? Scanf should do this! Why does it do that, instead? Because the manual said so. Have you read the manual? – autistic Mar 11 '13 at 3:03