Expected identifier in C

1.3k views Asked by At

The error I'm getting is "expected identifier or '(' before float." Any help would be appreciated. I've read it might have to do with the semi colon after fprod? I've tried adding parenthesis like (float) fsum and (float) fprod, as well as removing the semi colon both with and without parenthesis.

int main (void)

{

int x, y, z, fx, fy, fz, sum, prod, float fsum, float fprod;
                                   //The error occurs on line 9. The above is line 9
    printf("\n Enter the first integer number: ");
    scanf("%d", &x);
            if( x<=0){
            printf("\n Invalid entry. Entry must be greater than 0.");
            scanf("%d",&x);
    }

    printf("\n Enter the second integer number: ");
    scanf("%d", &y);
            if( y<=0){
            printf("\n Invalid entry. Entry must be greater than 0.");
            scanf("%d",&y);
    }
1

There are 1 answers

29
rost0031 On

You can't declare different types of variables in the same declaration. Change

int x, y, z, fx, fy, fz, sum, prod, float fsum, float fprod;

to

int x, y, z, fx, fy, fz, sum, prod;
float fsum, fprod;

As some comments have pointed out, it's bad practice to just group all the variables into a single declaration. It's better to split them up based on function or locality. This is really dependant on your program but you can do something like this:

int x, y, z;
int fx, fy, fz;
int sum, prod;
float fsum, fprod;

Also, it's a good idea to initialize your variables to some sane values since they will contain random undefined values and if you try to use them without successfully setting them, you could end up with hard to find bugs. So:

int x = 0, y = 0, z = 0; // or some other value if it makes sense
int fx = 0, fy = 0, fz = 0;
int sum = 0, prod  = 0;
float fsum = 0.0, fprod = 0.0;