Home > Software engineering >  Getting Previous declaration is here Error in C
Getting Previous declaration is here Error in C

Time:04-16

Task: write a function to calculate the area of a square, rectangle, circle. My code is right. IDK why I am getting this error complete error

#include<stdio.h>
#include<math.h>
int square();
float airclearea();
int rectangle();
int main(){
    int s,r1,r2;
    float a;
    printf("Enter the side of square : ");
    scanf("%d",&s);
    printf("Enter the side of rectangle");
    scanf("%d %d",&r1,&r2);
    printf("Enter the radius of circle");
    scanf("%f",&a);
    printf("%d",square(s));
    printf("%d",rectangle(r1,r2));
    printf("%f",airclearea(a));
    return 0;
}
int square(int s){
  
    return s*s;
}
int rectangle(int r1, int r2){
  
    return r1*r2;
}
float airclearea(float a){
    return 3.14*a*a;
}

CodePudding user response:

Before main you declared the function airclearea without a parameter

float airclearea();

But you are calling the function with an argument of the type float

printf("%f",airclearea(a));

In this case the compiler performs default argument promotions and in the case of the function the argument of the type float is promoted to the type double.

So the compiler expects that the function is defined with a parameter of the type double. But the function is defined with a parameter of the type float

float airclearea(float a){
    return 3.14*a*a;
}

Either declare the function before main with the parameter of the type float

float airclearea( float );

or in its definition specify the parameter as having the type double.

float airclearea(double a){
    return 3.14*a*a;
}

In any case it is always better to provide function prototypes before referencing to functions.

CodePudding user response:

You originally defined float airclearea(); near the top of your code as having no parameters. At the bottom of your code, you redefined it as

float airclearea(float a){
    return 3.14*a*a;
}

The first definition defines float airclearea();, without parameters. Replace it with float airclearea(float a);. Your parameter is float a. You haven't shown your other error messages, but I would assume you are getting the same error with int rectangle(); and int square();. Add parameters to the first definitions of both those functions, or just move the main function down to the bottom of your code and remove the top three placeholder definitions.

  • Related