使用 scanf() 将数字解析为数字 [英] Parsing number into digits with scanf()

查看:52
本文介绍了使用 scanf() 将数字解析为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想输入一个长度为 10 位的数字,然后将这些数字放入一个数组中.但出于某种原因,我得到了这个似乎与我的输入无关的随机 2 位数字 (??).

I want to type in a number that is 10 digits long, then put the digits into an array. But for some reason, I get this random 2-digit numbers that seem to have nothing to do with my input (??).

char number[10];            //number containing 10 digits
scanf("%s",number);         //store digits of number
printf("%d\n",number[0]);   //print the 1st digit in the number
printf("%d\n",number[1]);   //print the 2nd digit in the number

这是我得到的:

Input:
1234567890

Output:
49
50

实际上,49 应该是 1,50 应该是 2.

Actually, 49 should be 1, and 50 should be 2.

推荐答案

警告!您的代码可能会调用 未定义的行为!

Warning! Your code may invoke undefined behaviour!

但我们稍后再谈.让我们先解决您的实际问题.

But we'll talk about it later. Let us address your actual question first.

这是对正在发生的事情的分步说明.您需要知道的第一件事是,C 中的每个字符文字对于编译器来说实际上都是一个整数.

Here is a step by step explanation of what is going on. The first thing you need to know is that every character literal in C is actually an integer for the compiler.

试试这个代码.

#include <stdio.h>

int main()
{
    printf("%d\n", sizeof '1');
    return 0;
}

输出为:

4

这表明字符文字 '1' 被编译器表示为 4 字节整数.现在,让我们看看 '1' 的这个 4 字节整数在这里使用下一个代码是什么.

This shows that the character literal '1' is represented as 4 byte integer by the compiler. Now, let us see what this 4 byte integer for '1' is using the next code here.

#include <stdio.h>

int main()
{
    int a = '1';
    printf("a when intepreted as int : %d\n", a);
    printf("a when intepreted as char: %c\n", a);
    return 0;
}

编译并运行它.你会看到这个输出.

Compile it and run it. You'll see this output.

a when intepreted as int : 49
a when intepreted as char: 1

我们学到了什么?

字符 '1' 在我的系统上表示为整数 49.您的系统也是如此.那是因为在我和你的系统中,编译器使用 ASCII 码表示整数,其中 '1' 是 49,'2' 是 50,'A' 为 65,'B' 为 66,依此类推.请注意,对于另一个系统,字符到这些代码的映射可能会有所不同.您永远不应该依赖这些整数代码来识别字符.

The character '1' is represented as the integer 49 on my system. This is so for your system too. That's because in my system as well as yours, the compiler is using ASCII codes for the integers where '1' is 49, '2' is 50, 'A' is 65, 'B' is 66, and so on. Note that the mapping of the characters to these codes could be different for another system. You should never rely on these integer codes to identify the characters.

因此,当我尝试将此值打印为整数(使用 %d 作为格式说明符)时,打印的是 '1' 的整数值,其中是 49.但是,如果我们将此值打印为字符(使用 %c 作为格式说明符),则打印的是整数代码为 49 的字符.换句话说,1 被打印出来.

So when I try to print this value as integer (using %d as the format specifier), well what gets printed is the integer value of '1' which is 49. However, if we print this value as a character (using %c as the format specifier), what gets printed is the character whose integer code is 49. In other words, 1 gets printed.

现在试试这个代码.

#include <stdio.h>

int main()
{
    char s[] = "ABC123";
    int i;
    printf("char  %%d  %%c\n");
    printf("----  --  --\n");
    for (i = 0; i < 6; i++) {
        printf("s[%d]  %d  %c\n", i, s[i], s[i]);
    }
    return 0;
}

现在你应该看到这个输出.

Now you should see this output.

char  %d  %c
----  --  --
s[0]  65  A
s[1]  66  B
s[2]  67  C
s[3]  49  1
s[4]  50  2
s[5]  51  3

现在有意义吗?当您要打印字符时,您需要使用 %c 格式说明符.仅当您想查看代表该字符的整数代码时,才应使用 %d.

Does it make sense now? You need to use the %c format specifier when you want to print the character. You should use %d only when you want to see the integer code that represents that character.

最后,让我们回到您的代码.这就是你修复它的方法.

Finally, let us come back to your code. This is how you fix it.

#include <stdio.h>

int main()
{
    char number[10];
    scanf("%9[^\n]", number);
    printf("%c\n", number[0]);
    printf("%c\n", number[1]);
    return 0;
}

有两点需要注意.

  • 我使用 %c 作为格式说明符来打印读取的数字的字符表示.
  • 我已将 scanf 的格式说明符更改为仅在字符不是换行符的情况下最多接受 9 个字符.这是为了确保用户不会因为输入远远超过 9 个字符的字符串而导致程序崩溃.为什么是 9 而不是 10?因为我们需要为空终止符留空数组的一个单元格.更长的输入将覆盖超出为 number 数组分配的 10 个字节的内存位置.此类缓冲区溢出会导致代码调用未定义行为,这可能导致崩溃或杀死您的猫.
  • I have used %c as the format specifier to print the character representation of the digits read.
  • I have altered the format specifier for scanf to accept at most 9 characters only where the characters are not newline characters. This is to make sure that a user cannot crash your program by inputting a string that is far longer than 9 characters. Why 9 instead of 10?. Because we need to leave one cell of the array empty for the null-terminator. A longer input would overwrite memory locations beyond the allocated 10 bytes for the number array. Such buffer overruns lead to code that invoke undefined behaviour which could either cause a crash or kill your cat.

这篇关于使用 scanf() 将数字解析为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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