scanf的问题(“%d \\\<br/>”,& i) [英] problems with scanf("%d\n",&i)
问题描述
对于此代码:
int i;
scanf("%d\n",&i);
我无法停止我的程序,直到我输入两个数字。
I am not able to stop my program until I input two numbers.
我认为很奇怪,我知道当输入合适时,scanf将返回1.
当我输入12aEnter',12Enter'2等等,这是确定,i = 12,似乎当我输入的东西是不同的int或输入一个'输入'和另一个,scanf返回1。
I think it is very strange ,I know when the input is suitable,the scanf will return 1. When I input "12a 'Enter'","12 'Enter'2" and so on ,it is ok,the i=12,it seems that when I input something is different int or input a 'Enter' and something another,the scanf returns 1.
我缺少什么?
推荐答案
我无法停止我的程序,当我使用 scanf(%d\\\
,& i);
虽然这种格式使 scanf
读取数字并将其存储到 i
中,此读取继续,并持续到非空白字符后面跟 \\\
。这是为什么输入
1 2
使得 scanf
停止的原因。
"I am not able to stop my program until I input two numbers when I use scanf("%d\n",&i);
"
Although this format makes scanf
read the number and store it into i
, this "reading" continues and it lasts till non-whitespace character followed by \n
is found. This is the reason why input 1 2
makes this scanf
stop.
在这种情况下,不应在输入格式中指定换行符。
改用 scanf(%d,& i);
。
You should not specify newline in the input format in this case.
Use scanf("%d",&i);
instead.
这篇关于scanf的问题(“%d \\\<br/>”,& i)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!