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) 的问题


基础教程推荐
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 从 std::cin 读取密码 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01