所以我正在尝试编写一个简单的除法计算器,其中 a / b = c,结果将是 3 位数字,这是我的代码。
#include <stdio.h>
#include <stdlib.h>
int main()
{
float a;
float b;
printf("Enter the 1st number:");
scanf("%f", &a);
printf("Now the 2nd:");
scanf("%f", &b);
float c[3]; = (float)a / (float)b;
printf("The result is: %f!", c);
return 0;
}
然后在尝试编译时,你瞧,出现了这个错误。你能告诉我如何修复它吗?请说得简单一点,我还在学习。谢谢!
runcal.c:18:25:错误:初始化程序无效 18 | 浮点 c[3] = (浮点)a / (浮点)b; |
之前我在没有[3]的情况下尝试过,它有效,但它不是三位数。我尝试添加 strcpy() 但无济于事。
您无法指定数字的精度位数。您始终可以获得最高的精度。打印时可以选择精度。
float c = a / b; // c is a regular float; and b are floats to begin with, no need to cast them
printf("The result is: %.3f!", c); // use 3 digits after the decimal pointing, with rounding
// ^^