简体   繁体   English

scanf整数的每个数字并将整数存储在C中

[英]scanf each digit of integer AND store the whole number in C

Example input: 12345 输入示例:12345

As you can see I can store each digit "1" "2" "3" "4" "5" through scanf but how do I store the whole number "12345"? 如您所见,我可以通过scanf存储每个数字“ 1”,“ 2”,“ 3”,“ 4”,“ 5”,但是如何存储整数“ 12345”? Can it happen within the same scanf line? 它可以在同一scanf行中发生吗?

#include <stdio.h>
#include <math.h>


int main(){

    int wholeNumber = 0;
    int i1,i2,i3,i4,i5 = 0;

    printf("\nPlease enter a five digit integer value.\n");
    scanf("%1d%1d%1d%1d%1d",&i,&i2,&i3,&i4,&i5); //scanning each digit but 
                                                 //how do I store the whole #?
    return 0;
}

I will try to answer your first question. 我将尝试回答您的第一个问题。 You can simply multiply the entered numbers by multiples of 10. Lets show it with calculation: 您可以简单地将输入的数字乘以10的倍数。让我们通过计算来显示它:

5*1 + 4*10 + 3*100 + 2*1000 + 1*10000 = 12345 5 * 1 + 4 * 10 + 3 * 100 + 2 * 1000 + 1 * 10000 = 12345

So, here is the code for a manual use: 因此,这是手动使用的代码:

...
scanf("%1d%1d%1d%1d%1d",&i,&i2,&i3,&i4,&i5);
i*=10000;
i2*=1000;
i3*=100;
i4*=10;
i5*=1;
...

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

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