problems with scanf(quot;%dquot;,amp;i)(scanf(%d,amp;i) 的问题)
问题描述
对于此代码:
int i;
scanf("%d
",&i);
在我输入两个数字之前,我无法停止我的程序.
I am not able to stop my program until I input two numbers.
我觉得很奇怪,我知道什么时候输入合适,scanf会返回1.当我输入12a 'Enter'"、12 'Enter'2"等时,没关系,i = 12,似乎当我输入不同的int或输入'Enter'和另一个时,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
",&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
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 ",&i) 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:scanf("%d ",&i) 的问题
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
