简体   繁体   English

帮助scanf中的格式字符串

[英]Help with format string in scanf

Here's the statement: 声明如下:

int i;
scanf("%d ",&i);

Why does the space at the end of the format string causes scanf to accept two inputs instead of 1? 为什么格式字符串末尾的空格会使scanf接受两个输入而不是1?

The space at the end of the format string tells scanf to eat whitespace after the first integer. 格式字符串末尾的空格告诉scanf在第一个整数之后占用空白。 It's not actually accepting a second input. 它实际上并不接受第二个输入。 When you do enter a second value, scanf sees that the whitespace is finished it returns, storing the first integer into your variable i. 当您输入第二个值时,scanf会看到空格已完成,它将返回,并将第一个整数存储到变量i中。 The "second input" is still in the standard input stream. “第二个输入”仍在标准输入流中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM