打印 10 的幂 - C 中的 Printf

RPA*_*ion 5 c printing printf

我必须打印 0-19 之间的 10 的幂。问题是当我想显示第一个幂(应该是 1 )时(10^0),我只是无法强制不printf重复 0 。我只能printf在我的程序中使用一个和一个{ }块(这是主函数)。

#include <stdio.h>

int main(void) {
    int power = 19;
    for (int i = 0; i <= power; i++)
        printf("1%0*d\n",i, 0);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的输出:

10 // this should be 1, but printf still puts 0 here
10
100
1000
10000
100000
1000000
10000000
100000000
1000000000
10000000000
100000000000
1000000000000
10000000000000
100000000000000
1000000000000000
10000000000000000
100000000000000000
1000000000000000000
10000000000000000000
Run Code Online (Sandbox Code Playgroud)

dbu*_*ush 4

不要使用%d,而是使用%s可变精度,并让它从一串零打印。如果精度为 0,则不打印任何字符。

#include <stdio.h>

int main(void) {
    int power = 19;
    char zeros[] = "00000000000000000000";
    for (int i = 0; i <= power; i++)
        printf("1%.*s\n",i, zeros);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)