格式化输出的问题
#include <stdio.h>#define initial_balance 100.0
#define N 5
#define initial_profit 6
int main(void)
{
float a;
for (int i=0; i<N; i++){
a = initial_balance;
}
printf("Years");
for (int i=0; i<N; i++){
printf("\t%3d%% ", initial_profit + i);
}
printf("\n");
for (int i=0; i<N; i++){
for (int n=0, profit = initial_profit; n<N; n++){
printf("%d", i+1);
printf("\t%7.2f", a += (a * profit / 100));
profit++;
}
printf("\n");
}
return 0;
}
运行结果如图所示,我明明用了%7.2f设置了只保留两位小数, 为什么还会出现三位??
7.2
占7个 这应该是 bug 吧,我无法解释这一现象
对于这个程序,去掉前面的 printf("%d", i+1);,输出就正常了
$ cat main.c
#include <stdio.h>
#define initial_balance 100.0
#define N 5
#define initial_profit 6
int main(void)
{
float a;
for (int i=0; i<N; i++){
a = initial_balance;
}
printf("Years");
for (int i=0; i<N; i++){
printf("\t%3d%% ", initial_profit + i);
}
printf("\n");
for (int i=0; i<N; i++){
for (int n=0, profit = initial_profit; n<N; n++){
printf("%d", i+1);
printf("\t%7.2f", a += (a * profit / 100));
profit++;
}
printf("\n");
}
return 0;
}
$ gcc -g -Wall -o main main.c
$ ./main
Years 6% 7% 8% 9% 10%
1 106.001 107.001 108.001 109.001 110.00
2 112.362 114.492 116.642 118.812 121.00
3 119.103 122.503 125.973 129.503 133.10
4 126.254 131.084 136.054 141.164 146.41
5 133.825 140.265 146.935 153.865 161.05
$
$ cat main.c
#include <stdio.h>
#define initial_balance 100.0
#define N 5
#define initial_profit 6
int main(void)
{
float a;
for (int i=0; i<N; i++){
a = initial_balance;
}
printf("Years");
for (int i=0; i<N; i++){
printf("\t%3d%% ", initial_profit + i);
}
printf("\n");
for (int i=0; i<N; i++){
for (int n=0, profit = initial_profit; n<N; n++){
//printf("%d", i+1);
printf("\t%7.2f", a += (a * profit / 100));
profit++;
}
printf("\n");
}
return 0;
}
$ gcc -g -Wall -o main main.c
$ ./main
Years 6% 7% 8% 9% 10%
106.00107.00108.00109.00110.00
112.36114.49116.64118.81121.00
119.10122.50125.97129.50133.10
126.25131.08136.05141.16146.41
133.82140.26146.93153.86161.05
$
本帖最后由 人造人 于 2021-9-8 14:42 编辑
但是 %d 配 %\t7.2f 没问题
$ cat main.c
#include <stdio.h>
int main(void)
{
printf("%d", 1234);
printf("\t%7.2f", (float)123.456);
return 0;
}
$ gcc -g -Wall -o main main.c
$ ./main
1234 123.46$
"\t%3d%% 配 %d 配 %\t7.2f 也没问题
$ cat main.c
#include <stdio.h>
int main(void)
{
printf("\t%3d%% ", 1234);
printf("%d", 1234);
printf("\t%7.2f", (float)123.456);
return 0;
}
$ gcc -g -Wall -o main main.c
$ ./main
1234% 1234123.46$
复制一行 printf("\t%7.2f", a += (a * profit / 100));
第二行显示错误
$ cat main.c
#include <stdio.h>
#define initial_balance 100.0
#define N 5
#define initial_profit 6
int main(void)
{
float a;
for (int i=0; i<N; i++){
a = initial_balance;
}
printf("Years");
for (int i=0; i<N; i++){
printf("\t%3d%% ", initial_profit + i);
}
printf("\n");
for (int i=0; i<N; i++){
for (int n=0, profit = initial_profit; n<N; n++){
printf("%d", i+1);
printf("\t%7.2f", a += (a * profit / 100));
printf("\t%7.2f", a += (a * profit / 100));
profit++;
}
printf("\n");
}
return 0;
}
$ gcc -g -Wall -o main main.c
$ ./main
Years 6% 7% 8% 9% 10%
1 106.00112.361 107.00114.491 108.00116.641 109.00118.811 110.00121.00
2 119.10126.252 122.50131.082 125.97136.052 129.50141.162 133.10146.41
3 133.82141.853 140.26150.073 146.93158.693 153.86167.713 161.05177.16
4 150.36159.384 160.58171.824 171.38185.094 182.80199.264 194.87214.36
5 168.95179.085 183.85196.725 199.90215.895 217.19236.745 235.79259.37
$
再复制一行
第三行不对
$ cat main.c
#include <stdio.h>
#define initial_balance 100.0
#define N 5
#define initial_profit 6
int main(void)
{
float a;
for (int i=0; i<N; i++){
a = initial_balance;
}
printf("Years");
for (int i=0; i<N; i++){
printf("\t%3d%% ", initial_profit + i);
}
printf("\n");
for (int i=0; i<N; i++){
for (int n=0, profit = initial_profit; n<N; n++){
printf("%d", i+1);
printf("\t%7.2f", a += (a * profit / 100));
printf("\t%7.2f", a += (a * profit / 100));
printf("\t%7.2f", a += (a * profit / 100));
profit++;
}
printf("\n");
}
return 0;
}
$ gcc -g -Wall -o main main.c
$ ./main
Years 6% 7% 8% 9% 10%
1 106.00112.36119.101 107.00114.49122.501 108.00116.64125.971 109.00118.81129.501 110.00121.00 133.10
2 126.25133.82141.852 131.08140.26150.072 136.05146.93158.692 141.16153.86167.712 146.41161.05 177.16
3 150.36159.38168.953 160.58171.82183.853 171.38185.09199.903 182.80199.26217.193 194.87214.36 235.79
4 179.08189.83201.224 196.72210.49225.224 215.89233.16251.824 236.74258.04281.274 259.37285.31 313.84
5 213.29226.09239.665 240.98257.85275.905 271.96293.72317.225 306.58334.17364.255 345.23379.75 417.72
$
我认为这是 bug,我无法解释这一现象
页:
[1]