C语言 如何从 C 中的 stdio 获取 int?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5087062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get an int from stdio in C?
提问by ma?ek
I have significant trouble with this...
我有很大的麻烦...
printf("> ");
int x = getchar();
printf("got the number: %d", scanf("%d", &x));
Output
输出
> 1234
got the number: 1
回答by templatetypedef
I'm not fully sure that this is what you're looking for, but if your question is how to read an integer using <stdio.h>, then the proper syntax is
我不完全确定这就是您要查找的内容,但是如果您的问题是如何使用 读取整数<stdio.h>,那么正确的语法是
int myInt;
scanf("%d", &myInt);
You'll need to do a lot of error-handling to ensure that this works correctly, of course, but this should be a good start. In particular, you'll need to handle the cases where
当然,您需要进行大量错误处理以确保它正常工作,但这应该是一个好的开始。特别是,您需要处理以下情况
- The
stdinfile is closed or broken, so you get nothing at all. - The user enters something invalid.
- 该
stdin文件已关闭或损坏,因此您什么也得不到。 - 用户输入的内容无效。
To check for this, you can capture the return code from scanflike this:
要检查这一点,您可以scanf像这样捕获返回码:
int result = scanf("%d", &myInt);
If stdinencounters an error while reading, resultwill be EOF, and you can check for errors like this:
如果stdin在阅读时遇到错误,result将会是EOF,您可以检查如下错误:
int myInt;
int result = scanf("%d", &myInt);
if (result == EOF) {
/* ... you're not going to get any input ... */
}
If, on the other hand, the user enters something invalid, like a garbage text string, then you need to read characters out of stdinuntil you consume all the offending input. You can do this as follows, using the fact that scanfreturns 0 if nothing was read:
另一方面,如果用户输入了无效的内容,例如垃圾文本字符串,那么您需要读取字符,stdin直到您使用所有有问题的输入。您可以按如下方式执行此操作,使用scanf如果未读取任何内容则返回 0的事实:
int myInt;
int result = scanf("%d", &myInt);
if (result == EOF) {
/* ... you're not going to get any input ... */
}
if (result == 0) {
while (fgetc(stdin) != '\n') // Read until a newline is found
;
}
Hope this helps!
希望这可以帮助!
EDIT: In response to the more detailed question, here's a more appropriate answer. :-)
编辑:为了回答更详细的问题,这里有一个更合适的答案。:-)
The problem with this code is that when you write
这段代码的问题是,当你写
printf("got the number: %d", scanf("%d", &x));
This is printing the return code from scanf, which is EOFon a stream error, 0if nothing was read, and 1otherwise. This means that, in particular, if you enter an integer, this will always print 1because you're printing the status code from scanf, not the number you read.
这是打印从返回代码scanf,这是EOF一个流错误,0如果没有被读取,以及1其他。这意味着,特别是,如果您输入一个整数,这将始终打印,1因为您正在打印来自 的状态代码scanf,而不是您读取的数字。
To fix this, change this to
要解决此问题,请将其更改为
int x;
scanf("%d", &x);
/* ... error checking as above ... */
printf("got the number: %d", x);
Hope this helps!
希望这可以帮助!
回答by Jerry Coffin
The typical way is with scanf:
典型的方法是scanf:
int input_value;
scanf("%d", &input_value);
In most cases, however, you want to check whether your attempt at reading input succeeded. scanfreturns the number of items it successfully converted, so you typically want to compare the return value against the number of items you expected to read. In this case you're expecting to read one item, so:
但是,在大多数情况下,您希望检查读取输入的尝试是否成功。scanf返回它成功转换的项目数,因此您通常希望将返回值与您希望读取的项目数进行比较。在这种情况下,您希望阅读一项,因此:
if (scanf("%d", &input_value) == 1)
// it succeeded
else
// it failed
Of course, the same is true of all the scanffamily (sscanf, fscanfand so on).
当然,所有的scanf家庭都是如此(sscanf,fscanf等等)。
回答by josegomezr
The solution is quite simple ... you're reading getchar() which gives you the first character in the input buffer, and scanf just parsed it (really don't know why) to an integer, if you just forget the getchar for a second, it will read the full buffer until a newline char.
解决方案很简单......你正在阅读 getchar() 它给你输入缓冲区中的第一个字符,而 scanf 只是将它(真的不知道为什么)解析为一个整数,如果你只是忘记了 getchar一秒钟,它将读取完整的缓冲区,直到换行符。
printf("> ");
int x;
scanf("%d", &x);
printf("got the number: %d", x);
Outputs
输出
> [prompt expecting input, lets write:] 1234 [Enter]
got the number: 1234

