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
  • 1 float c[3] declares an array with three elements. c[0], c[1], and c[2] will be the resulting variables, each of them of type float. 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
  • 1 You seem to have skipped most early parts of your beginners books, tutorials or classes. Don't do that. And is the code you show the one you actually try to build? The one you currently show will lead to a very different error. – Some programmer dude Commented 1 hour ago
  • 2 And what do you think the -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
  • 3 @Rain Zhang What is the semicolon doing here float c[3]; = (float)a / (float)b;? – Vlad from Moscow Commented 1 hour ago
Add a comment  | 

2 Answers 2

Reset to default 3

float 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