C 输入数字(可能带前导零),但输出数字时不带前导零
我需要保留输入中的任何前导零,因此我将数字作为C 输入数字(可能带前导零),但输出数字时不带前导零,c,C,我需要保留输入中的任何前导零,因此我将数字作为chars,然后使用ctoi()函数将它们转换回整数,如以下代码所示: #include<stdio.h> #define ctoi(a) a-'0' int main() { int n; char ch; scanf("%c",&n); ch=ctoi(n); printf("%d",n); } 将数字存储为整数时,只存储实际数字,而不存储用于生成该数字的格式。如果要保持格式不
char
s,然后使用ctoi()
函数将它们转换回整数,如以下代码所示:
#include<stdio.h>
#define ctoi(a) a-'0'
int main() {
int n;
char ch;
scanf("%c",&n);
ch=ctoi(n);
printf("%d",n);
}
将数字存储为整数时,只存储实际数字,而不存储用于生成该数字的格式。如果要保持格式不变,则需要将其存储为字符串或其他存储原始格式的方法
int n = 10; // only stores a number in memory
char text[10] = "00010"; // stores any text, but can not be used for number arithmetic as stored here
你需要一份工作。检查表中的所有格式代码。更糟糕的是,OP使用的是单个字符,而不是数组,因此只保留第一位数字。
int n = 10; // only stores a number in memory
char text[10] = "00010"; // stores any text, but can not be used for number arithmetic as stored here