-2

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;
}
4
  • You haven't shown the complete error — it's text that should be in the question. Commented Apr 15, 2022 at 14:36
  • int square(); means it takes no arguments. Update those signatures or move those functions above main() & get rid of prototypes. Don't share images of text/code/logs. Commented Apr 15, 2022 at 14:36
  • 1
    The 3 declarations int square(); float airclearea(); int rectangle(); have different meanings in C and C++ — are you perchance using a C++ compiler? In C++, the functions are declared to take no arguments, and the later definitions contradict those declarations. In C, which is the tagged language, the three statements declare the functions but do not specify a prototype. My best guess is that you're using a C++ compiler to compile C code; that doesn't work. Even in C, it is horribly sloppy practice not to declare the functions with full prototypes with the correct argument lists. Commented Apr 15, 2022 at 14:38
  • 1
    @SparKot: You are confusing C and C++. Your statement about 'no arguments' is accurate for C++; it is not accurate for C. The comment about "don't share images" is accurate. Commented Apr 15, 2022 at 14:40

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

-1

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.

5 Comments

You're describing C++, not C. The first declaration, in C, says that airclearare() returns a float and takes an indeterminate (unspecified) set of arguments (but it isn't a variadic function like printf(); the full prototype does not end , ...)).
Oh- I see now. Then I would guess that the correct answer would be to move main down to the bottom and remove the top three declarations.
Moving main() to the bottom and removing the declarations is a perfectly viable solution. The alternative is to complete the declarations above main() with the types of the parameters. Both work. There are different schools of thought on 'main() at the top' and 'main() at the bottom' with strong protagonists and reasonable arguments on both sides. The main rule is "be consistent". (I usually put main() at the bottom of a single-file program, but at the top of the file if its source code is just one of many that constitute the complete program. Sort of "have your cake and eat it".)
float airclearea(float a); adding (float a) removed my error . I though while declaring or naming the function there's no need of adding a parameter . But in case of square and rectangle IDK why it's working
@numextrix: it 'works' for square() and rectangle() because the actual parameters are of type int, and int is not subject to promotion under the 'default argument promotion rules'. However, when there is no prototype in scope, arguments of type float are promoted to double, so when the code comes across the definition float airclearAra(float a), there are problems because the calling code will have promoted the argument but the function definition says that was incorrect. Always declare functions with full prototypes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.