我想输入一个10位长的数字,然后将这些数字放入一个数组中。但由于某种原因,我得到了这个随机的 2 位数字,它们似乎与我的输入无关(??)。
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。
最佳答案
您正在获取字符 1
和 2
的 ASCII 值。使用 %c
说明符打印数字。
printf("%c\n",number[0]);
关于c - 使用 scanf() 将数字解析为数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22987166/