简体   繁体   English

将数字的数字打印为字符的程序(C语言)

[英]Program that prints the digits of a number as characters (C language)

I have to do a program to take a number and print all the digits one by one as characters. 我必须做一个程序来获取一个数字,并将所有数字一一打印为字符。

The code I have written is here: 我写的代码在这里:

   #include <stdio.h>
   #include <stdlib.h>
   char digito(int, int);
   int numberofdigits(int);

   int main(void)
{
int num=0, test=0, kdigits=0, k=0;
char ch='\0';
printf("Enter a positive number:\n");
test=scanf("%d", &num);
if (teste!=1 || num<=0)
{
    printf("Error: not a valid number.\n");
    exit(EXIT_FAILURE);
}

kdigits = numberofdigits(num);


for(k=0; k<kdigits; k++)
{
    ch= digito(num, k);
    printf("The digit %d of the number is %c\n", k, ch);
}

return EXIT_SUCCESS;
}


int numberofdigits(int _n)
{
int count=0;
while (_n!=0)
{
    _n/=10;
    count++;
}

return count;
}


char digito(int _number, int _kdigit)
{
int flag=0, digit=0;
char ch='\0';
for (flag=0; flag<=_kdigit; flag++)
{
    digit=_number%10;
    _number/=10;
}

ch= digit + '0';
return ch;
}

Now the code is working pretty fine for relatively small numbers (up to 8 or 9 digits I would say). 现在,该代码对于相对较小的数字(我会说最多8或9位数字)可以正常工作。

But then something odd is happening: I tried to print the digits of the number 11111111111122 and obtained 但是随后发生了一些奇怪的事情:我尝试打印数字11111111111122的数字并获得

The digit 0 of the number is 3; 数字的数字0是3;数字0是3。

The digit 1 of the number is 7; 该数字的数字1是7;

The digit 2 of the number is 3; 该数字的数字2是3;

The digit 3 of the number is 6; 该数字的数字3是6;

The digit 4 of the number is 1; 该数字的数字4为1;

The digit 5 of the number is 7; 该数字的数字5是7;

The digit 6 of the number is 0; 该数字的数字6为0;否则为0。

The digit 7 of the number is 3; 该数字的数字7是3;

I wonder why? 我想知道为什么? Is it because it's a very large number? 是因为数量很大吗? Because I tried even larger numbers and what happens is that the program enters the initial if clause that verifies the scanf reading. 因为我尝试了更大的数字,结果是程序输入了初始的if子句来验证scanf的读数。 And that's ok. 没关系。 But the problem is that the program should also do the same with this number since it's bigger that the largest int. 但是问题在于程序也应该对此数字执行相同的操作,因为它比最大的int大。 Can someone help me fix this please? 有人可以帮我解决这个问题吗?

Thanks! 谢谢!

There are two answers to two questions: 有两个问题的两个答案:
Q1) Why do I get wrong digits for the example? Q1)为什么我的示例数字错误?
A1) As others have commented: Because the example number is too high for the data type used, it matches the number which you find in the lower 32 bits: 30716370. A1)正如其他人所评论的:由于示例数字对于所使用的数据类型而言过高,因此它与您在低32位中找到的数字匹配:30716370。

Q2) Why is the initial check "<0" not triggering for the example but does trigger for other, higher examples? Q2)为什么最初的检查结果“ <0”不触发该示例,但触发其他较高的示例?
A2) Because the 30716370 is smaller than the biggest positive number which can be represented by a 32 bit signed int, which is 2147483647 == 0x7FFFFFFF. A2)因为30716370小于可以由32位带符号的int表示的最大正数,即2147483647 == 0x7FFFFFFF。 That one however is smaller (even in the number of digits, but that is not the point) than 11111111111122. 但是,该数字小于11111111111122(即使是位数,但这也不是重点)。
30716370 < 30716370 <
2147483647 < 2147483647 <
11111111111122 11111111111122
The even higher numbers will by chance have the bit31 set, which makes the lower 32bit look negative. 更高的数字将偶然设置bit31,这使较低的32bit看起来为负数。 You could probably find other numbers which are too high but do not seem negative. 您可能会发现其他数字过高,但看起来不是负数。

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

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