admin管理员组文章数量:1123052
So I'm trying to code a simple division calculator where a / b = c, the result will be 3 digits, here's my code.
#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;
}
Then upon trying to compile, lo and behold, this error shows. Can you tell me how to fix it? Please make it simple, I'm still learning. Thanks!
runcal.c:18:25: error: invalid initializer 18 | float c[3] = (float)a / (float)b; |
Earlier I tried it without the [3], it worked, but it was not three digits. I tried adding strcpy() but to no avail.
So I'm trying to code a simple division calculator where a / b = c, the result will be 3 digits, here's my code.
#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;
}
Then upon trying to compile, lo and behold, this error shows. Can you tell me how to fix it? Please make it simple, I'm still learning. Thanks!
runcal.c:18:25: error: invalid initializer 18 | float c[3] = (float)a / (float)b; |
Earlier I tried it without the [3], it worked, but it was not three digits. I tried adding strcpy() but to no avail.
Share Improve this question edited 1 hour ago wohlstad 27k16 gold badges54 silver badges84 bronze badges asked 1 hour ago Rain ZhangRain Zhang 11 silver badge2 bronze badges 4 |2 Answers
Reset to default 3float c[3]
declares an array of three float values, not a single float. In C, you can’t assign a single division result to an array in that manner. The compiler is telling you that this is not a valid way to initialize an array.
If you want to display your division result with three decimal places, you don’t actually need an array. Just store the result in a single float
variable and tell printf
to show three digits after the decimal point.
Just use the printf
format specifier %.3f
to show three digits after the decimal point.
#include <stdio.h>
#include <stdlib.h>
int main()
{
float a;
float b;
float c; // Just a single float
printf("Enter the 1st number:");
scanf("%f", &a);
printf("Now the 2nd:");
scanf("%f", &b);
c = a / b; // Perform the division
// Print the result with exactly 3 decimal digits
printf("The result is: %.3f\n", c);
return 0;
}
You cannot specify the number of digits of precision of a number. You always get the maximum precision available (approx 7/8 digits for float
, or 15/16 digits for double
). When printing you can choose the precision.
float c = a / b; // c is a regular float; a and b are floats to begin with, no need to cast them
printf("The result is: %.3f!\n", c); // use 3 digits after the decimal point, with rounding
// ^^
Note: when working with floating-point values it's best to use double
(unless there's a strong reason to do otherwise)
本文标签: linuxWhen Coding in Ccompiling in gcc with o flag got Error Invalid InitializerStack Overflow
版权声明:本文标题:linux - When Coding in C, compiling in gcc with -o flag got Error: Invalid Initializer - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736541985a1944396.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
float c[3]
declares an array with three elements.c[0]
,c[1]
, andc[2]
will be the resulting variables, each of them of typefloat
. There is no way to declare a variable with a specific number of digits in C using only primitive types. – giusti Commented 1 hour ago-o
flag does or mean? Why do you seem to think it's involved in the error you have? – Some programmer dude Commented 1 hour ago