简体   繁体   English

使用%d扫描字符

[英]Scanf a char using %d

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. 我在下面有一个特定的示例,如果我输入整数(请参见output1),则该示例工作得很好,当我尝试在scanf函数调用中使用%d说明符扫描char时,得到以下output2。

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. 因此,我的问题是,如果输入一个char我希望类型说明符将其转换为等效的int值(如果不是垃圾值),即使在任何情况下都应打印/ segfault。 But, here I'm getting continuous prints which I feel is wrong as scanf is getting bypassed every single time. 但是,在这里我得到的是连续打印,这是错误的,因为scanf每次都被绕过。 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\n", a);
   }

return 0;
}

Output1 : 输出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:

Output2 : for input a 输出2 :用于输入

>     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: 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. PS:我知道这是一个愚蠢的问题,询问在无效的情况下会发生什么情况,在这种情况下,非预期的类型说明符(在这种情况下为%d)用于不同的类型,但是我想知道在后台发生了什么(如果有)。 Thanks 谢谢

You may check scanf as @Some programmer dude . 您可以将scanf视作@Some程序员dude You may compare the count arguments succesfully filled (Thanks to @chux ) 您可以比较成功填充的count参数(感谢@chux

In your case, scanf didn't find any integer value, reached the end of the input and returned EOF, keeping the a variable untouched. 在你的情况和scanf函数,没有发现任何整数值,达到了输入的末尾,EOF返回,保持a变量不变。

On failure it'll return EOF (read http://www.cplusplus.com/reference/cstdio/scanf/#return ). 如果失败,它将返回EOF(请阅读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\n", a);

For characters, you better use getch() or at least, ask for "%c" on scanf: 对于字符,最好使用getch()或至少在scanf上要求"%c"

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

The "junk" value you recieve is what was in your program memory, because a is not initialized. 您收到的“垃圾”值就是程序存储器中的值,因为a没有初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM