scanf("%d\n",&n);
在实际输入后跳过任意数量的尾随空格(包括无)。也可以写成scanf("%d ",&n);
.
scanf("\n%c",&ch);
在实际输入之前跳过任意数量的前导空格(包括无)。也可以写成scanf(" %c",&ch);
.
NOTE:格式说明符中的空格可以跳过任意数量的空格。
现在这是什么意思跳过空格 ?
它的意思是scanf
重复从输入中读取空白字符,直到达到非空白特点。现在缓冲区中没有剩余空白字符.
当它遇到一个非空白字符,那么这个字符就是put back在扫描下一个输入项或在下一次调用期间再次读取scanf
.
现在回答你的问题。
为什么要交错scanf()
+ printf()
语句导致两者scanf()
首先调用执行,然后调用两者printf()
打电话?
I am assuming the input for n
is 15
. When you press Enter key then the \n
character goes with 15
in the input buffer. scanf("%d\n",&n);
reads the 15
and then skips \n
. Now this scanf
waits for a non-white-space character to be entered (unlike what you supposed that 15
should be printed) . When you enter a
, it puts it back for the next call of scanf
. The next statement scanf("\n%c",&ch);
reads this a
from the buffer and do not let the user to input the value for ch
. Since the value of n
and ch
both is now read by these scanf
s, it appears to be that both of
printf("d-%d \n",n);
printf("ch-%d \n",ch);
在两者之后执行scanf
s 调用(事实并非如此!)。