Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 输入数字(可能带前导零),但输出数字时不带前导零_C - Fatal编程技术网

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