使用 %d 扫描一个字符 [英] Scanf a char using %d

查看:19
本文介绍了使用 %d 扫描一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有一个具体的例子,如果输入整数(见输出 1),它工作得非常好,当我尝试在 scanf 函数调用中使用 %d 说明符扫描字符时,我得到下面的输出 2.

I have a specific example below, which works perfectly fine if integers are inputted (see output1), when I try to scan a char using %d specifier in scanf function call I get the output2 below.

所以,我的问题是,如果输入一个 char,我希望类型说明符应该将其转换为等效的 int 值,如果不是垃圾值,即使在任何一种情况下它都应该打印/段错误.但是,在这里我得到了连续的打印,我觉得这是错误的,因为 scanf 每次都被绕过.我很不确定后台发生了什么,我也想知道.

So, my question is if input a char I hope the type specifier should convert it to an equivalent int value, if not a junk value, even in the either case it should print/segfault. But, here I'm getting continuous prints which I feel is wrong as scanf is getting bypassed every single time. I'm pretty unsure what's happening in the background and would like to know the same.

#include <stdio.h>

int main()
{

   int a;

   while (1){
       printf("enter a number:");
       scanf("%d", &a);
       printf("entered number is %d
", a);
   }

return 0;
}

输出 1:

>     enter a number:1
>     entered number is 1
>     enter a number:
>     3
>     entered number is 3
>     enter a number:4
>     entered number is 4
>     enter a number:5
>     entered number is 5 enter a number:

输出2:用于输入a

>     enter a number:entered number is 32767
>     enter a number:entered number is 32767
>     enter a number:entered number is 32767
>     enter a number:entered number is 32767
>     enter a number:entered number is 32767
>     enter a number:entered number is 32767
>     enter a number:entered number is 32767
>     enter a number:entered number is 32767
>     enter a number:entered number is 32767

PS:我知道这是一个愚蠢的问题,询问在类型说明符意外(在本例中为 %d)用于不同类型的无效情况下会发生什么,但我想知道在后台发生了什么,如果有的话.谢谢

PS: I know this is a stupid question of asking what happens in an invalid case where a type specifier unintended (%d in this case) is used for different type, but I would like to know what happens in the background, if any. Thanks

推荐答案

你可以检查scanf为@Some 程序员老兄.您可以比较成功填充的计数参数(感谢 @chux)

You may check scanf as @Some programmer dude. You may compare the count arguments succesfully filled (Thanks to @chux)

在您的情况下,scanf 没有找到任何整数值,到达输入的末尾并返回 EOF,保持 a 变量不变.

In your case, scanf didn't find any integer value, reached the end of the input and returned EOF, keeping the a variable untouched.

如果失败,它将返回 EOF(阅读 http://www.cplusplus.com/reference/cstdio/scanf/#return).

On failure it'll return EOF (read http://www.cplusplus.com/reference/cstdio/scanf/#return).

if(scanf("%d", &a) == 1) //Check if exactly one parameter was read. 
    printf("entered number is %d
", a);

对于字符,最好使用 getch() 或至少在 scanf 上请求 "%c":

For characters, you better use getch() or at least, ask for "%c" on scanf:

if(scanf("%c", &a) == 1)
    printf("entered key was %d
", a);

您收到的垃圾"值是程序内存中的值,因为 a 未初始化.

The "junk" value you recieve is what was in your program memory, because a is not initialized.

这篇关于使用 %d 扫描一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆