I was finishing up K&R exercises 7.4 and 7.5 and came across an annoying "feature" that I don't believe the standard states.
According to the K&R, the mode of action for the conversion specification "%c"
"The next input characters (default 1) are placed at the indicated spot. The normal skip over white space is suppressed; to read the next non-white space character, use %1s"
My question is, is that statement supposed to be read like:
"The next input characters (default 1) are placed at the indicated spot. THEN, in successive calls to scanf in which %c is used again, the normal skip over white space is suppressed; to read the next non-white space character, use %1s"
...because this code:
void test1()
{
char t1, t2;
scanf("%c %c", &t1, &t2);
printf("%d\n", t1);
printf("%d\n", t2);
//INPUT is: "b d" (without quotes)
}
results in t1 = 98 (b) and t2 = 100 (d). (Whitespace skipped)
However, this code:
void test2()
{
char t1, t2;
scanf("%c", &t1);
scanf("%c", &t2);
printf("%d\n", t1);
printf("%d\n", t2);
//INPUT is: "b d" (without quotes)
}
results in t1 = 98 (b) and t2 = 32 (' '). (Whitespace NOT skipped)
Reading the original quote, I think any reasonable person would take it to mean that during that same call to scanf(%c), the whitespace skip is suppressed. However, that doesn't seem to be the case.
It seems that in order to gain back the original functionality, one would have to completely empty stdin.
Is this supposed to work this way? Has it been documented? Because i've looked around and haven't seen much information on this.
For reference, I'm programming in C99.