为什么在 C 编程中 scanf("%d", &number) 结果 'a' 为 29? [英] Why scanf("%d", &number) results 'a' as 29 in C programming?

查看:50
本文介绍了为什么在 C 编程中 scanf("%d", &number) 结果 'a' 为 29?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 C 中有以下代码

I have a following code in C

#include <stdio.h>
int main()
{
    int number;

    // printf() displays the formatted output
    printf("Enter an integer: ");

    // scanf() reads the formatted input and stores them
    scanf("%d", &number);

    // printf() displays the formatted output
    printf("You entered: %d", number);
    return 0;
}

在这里我应该输入一个数字,但我输入了一个字符a",结果是 29.我预计它会抛出异常,但我对输出感到惊讶.

Here I am expected to enter a number but I have entered a character 'a' and it results 29. I expected that it will throw an exception but I was surprised by an output.

推荐答案

对于初学者来说,没有参数的函数 main 应声明为

For starters the function main without parameters shall be declared like

int main( void )

只需按照以下方式更改您的程序

Just change your program the following way

#include <stdio.h>

int main( void )
{
    int number;

    // printf() displays the formatted output
    printf("Enter an integer: ");

    // scanf() reads the formatted input and stores them
    if (scanf("%d", &number) == 1)
    {
        // printf() displays the formatted output
        printf("You entered: %d\n", number);
    }
    else
    {
        puts("Invalid input");
    }

    return 0;
}

看看如果您尝试输入字符 'a' 而不是数字会发生什么.

and see what will happen if you try to enter the character 'a' instead of a number.

根据C标准中函数的描述(7.21.6.2 fscanf函数)

According to the description of the function in the C Standard (7.21.6.2 The fscanf function)

d 匹配一个可选的有符号十进制整数,其格式为与
strtol 函数的主题序列的预期相同基本参数的值 10.相应的参数应为指向有符号整数的指针.

d Matches an optionally signed decimal integer, whose format is the same as expected for the subject sequence of the strtol function with
the value 10 for the base argument. The corresponding argument shall be a pointer to signed integer.

And(7.22.1.4 strtol、strtoll、strtoul 和 strtoull 函数)

And (7.22.1.4 The strtol, strtoll, strtoul, and strtoull functions)

7 如果主题序列为空或没有预期的形式,不进行任何转换; nptr 的值存储在endptr 指向的对象,前提是 endptr 不是 null指针.

7 If the subject sequence is empty or does not have the expected form, no conversion is performed; the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a null pointer.

因此在演示程序中将执行 else 语句,因为(C 标准,7.21.6.2 fscanf 函数)

Thus in the demonstrative program the else statement will be executed because (The C Standard, 7.21.6.2 The fscanf function)

16 fscanf 函数返回宏 EOF 的值,如果输入失败发生在第一次转换(如果有)完成之前.否则,函数返回分配的输入项数,如果出现以下情况,则可能会少于规定值,甚至为零早期匹配失败

16 The fscanf function returns the value of the macro EOF if an input failure occurs before the first conversion (if any) has completed. Otherwise, the function returns the number of input items assigned, which can be fewer than provided for, or even zero, in the event of an early matching failure

这篇关于为什么在 C 编程中 scanf("%d", &amp;number) 结果 'a' 为 29?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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