I'm using the following C code [duplicate] - c

This question already has answers here:
Getting "conflicting types for function" in C, why?
(11 answers)
Closed 5 years ago.
float pie = 3.14;
int main()
{
float r,res;
printf("Enter radius value\n");
scanf("%f", &r);
res=area( r );
printf("Area = %f\n",res);
//printf("Circumference = %f\n", circum( r ));
}
float area( float r )
{
return pie * r * r;
}
I'm getting error as conflicting types for 'area' and previous implicit declaration of 'area' was here,But this code works fine if i changed to integer
In IDE it works fine if i changed that code to integer float pie = 3.14;
int main() {
int r,res;
printf("Enter radius value\n");
scanf("%d", &r); res=area( r );
printf("Area = %d\n",res);
}
int area( int r )
{
return pie * r * r;
}
without declaration it works fine for integer but for floating point numbers i am getting error why?

C allows implicit function declaration, so because you didn't give an explicit function declaration prior to calling it for the first time, it's implicitly declared with this prototype:
int area(); // Implicit arguments
And later when you give a real declaration, they're conflict.
If you want the function to return an actual floating point number, give a forward declaration:
float area(float);
Add the above line before int main() and the problam is solved.
By the way, implicit function declaration is removed as of C11 standard (ISO/IEC 9899:2011).

Whenever you are using a common term or a variable all over the program
like you did here
float pie = 3.14
write this instead
#define pie 3.14 //this is a macro
and coming to your error ,
whenever you are using a function, you have to provide a explicit declaration of it or in other words prototype of it.
Type this on top of your C file
float area(float r)
or
float area(float)
And if you feel lazy then use your function before your main function instead using it after you main function.

You should do function call after function defination.Otherwise declar function in header file and use it.

Related

Getting Previous declaration is here Error in C

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;
}
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.
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.

too many arguments for function error but function clearly takes two arguments

code to ask user to input base and power and prints result:
#include <stdio.h>
int main()
{
float v;
int power;
printf("Enter value of x:\t");
scanf("%f", &v);
printf("Enter power:\t");
scanf("%d", &power);
v = exp(v, power);
printf("%.2f", v);
}
float exp(float n, int i) {
float base = n;
int power = i;
float result = 1;
while (power != 0)
{
result = result * base;
--power;
}
return result;
}
compiler spits out:
x.c:11:6: error: too many arguments to function ‘exp’
11 | v = exp(v, power);
| ^~~
clearly:
float exp(float n, int i) {
takes two arguments. What's going on here?
It's because your exp function is not known by the compiler yet.
exp is built-in function in C.
The function prototype of exp() is: double exp(double x);
To make your program work, you should put your exp function above main function.
Two things are happening here.
You have not declared or defined exp() before use
Your compiler has a built-in for exp() with a different signature
I am not sure what compiler you are using but I would be surprised if there were not also warnings explaining this. GCC produces:
main.c: In function ‘main’:
main.c:12:11: warning: implicit declaration of function ‘exp’ [-Wimplicit-function-declaration]
v = exp(v, power);
^~~
main.c:12:11: warning: incompatible implicit declaration of built-in function ‘exp’
main.c:12:11: note: include ‘’ or provide a declaration of ‘exp’
main.c:12:11: error: too many arguments to function ‘exp’
main.c: At top level:
main.c:18:9: warning: conflicting types for built-in function ‘exp’
float exp(float n, int i) {
^~~
Providing a prototype solves the problem:
float exp(float n, int i) ;
int main()
{
...
}
However it is probably ill-advised. The standard library function exp() computes e (Euler's number, 2.7182818...) raised to the given power the single argument, so is semantically different that your exp().
Overriding a standard library function in this way is in any case ill advised even if it has the same semantics. I strongly suggest that you use a different name such as power() for example (not pow() - that is also a standard library function).

Too many arguments to function call, What do I do?

I am working on a problem in my textbook and I need to make a triangle angle calculator Im used to java but Im not 100% on C yet, I don't understand the logic in it.
#include <stdio.h>
static float angleB;
static float angleA;
float remainingAngle(float answer)
{
float answer= angleA+angleB;
//redefinition of answer
return 0;
}
//CANT TOUCH FROM HERE TO END
int main(int argc, const char * argv[]) {
float angleA = 30.0;
float angleB = 60.0;
float angleC = remainingAngle(angleA,angleB);
// to many arguments to function call (referring to angleC)
printf("the third angle is %.2f\n",angleC);
return 0;
} //END CANT TOUCH
I don't know what to do here.
In your function definition
float remainingAngle(float answer)
the function remainingAngle() accepts one parameter.
OTOH, in your function call
remainingAngle(angleA,angleB);
you're passing two arguments.
The supplied argument number and type should match with the parameter list in function definition.
That said, your code is wrong.
Point 1. Your local variables will shadow the global ones. Maybe that's not what you want. Change
float angleA = 30.0;
float angleB = 60.0;
to
angleA = 30.0;
angleB = 60.0;
Point 2. The function
float remainingAngle(float answer)
{
float answer= angleA+angleB;
//redefinition of answer
return 0;
}
is logically wrong. It should rather be
float remainingAngle(void)
{
float answer= angleA+angleB;
return answer;
}
and should be called like
float angleC = remainingAngle();
AFTER EDIT:
as per your requirement, you can do
float remainingAngle(float angleA, float angleB)
{
float answer= angleA+angleB;
return answer;
}
However, this makes the global variables useless.
In addition to Souravs answer you could also do the following:
float remainingAngle(float angA, float angB)
{
float answer = angA + angB;
return answer;
}
So your call will stay the same.
And for the logical side, what you do is:
You pass two arguments (value of angleA and value of angleB) to the function remainingAngle. There you do the calculation and return the result (answer).
The design of your function isn't good:
static float angleB;
static float angleA;
float remainingAngle(float answer)
{
float answer= angleA+angleB;
//redefinition of answer
return 0;
}
What do I do?
There's no point is passing in the answer; the answer is what you want to receive. (There's also no point in passing "space" for the answer. The anser is a float, a scalar variable. Just pass that around.) Remove the answer argument.
You redefine answer as local variable. After removing the argument of the same name, that redefinition goes away. But in that case you should return answer instead of 0. You could even do without the intermediary variable answer and return the result expression. So: Return the desired expression
The answer depends on two global variables. The function should work on two angles that you pass as arguments. Provide two angle arguments to your function and remove the global variables.
And finally, the sum of the three angles in a triangle is 180°, so your remaining angle calculation is wrong.
Putting all that together:
float remainingAngle(float a, float b)
{
return 180.0f - a - b;
}
and call it like this:
float angleC = remainingAngle(angleA, angleB);
Im used to java
I'm not too familiar with Java, but I don't think functios on scalars like this one are fundamentally different in C and Java.

why this program is not showing any error?

in the code below .
i have defined function prototype with no argument
in definition as well as in function call i have used one parameter.
i would like to know why i am not getting any error ?
# include <stdio.h>
float circle(); /* no parameter*/
int main()
{
float area;
int radius =2;
area=circle(radius);
printf("%f \n",area);
return 0;
}
float circle( r) /* with one parameter even no parameter type */
{
float a;
a=3.14*r*r;
return (a);
}
The
float circle();
is not a function with zero parameters. It's a function with an unspecified number of parameters.
The
float circle( r) {
is a K&R-style definition in which the type of r defaults to int. See https://stackoverflow.com/a/18433812/367273
This is because compiler treat r as int by default when no parameter is defined for circle. Try to run your code after declaring function prototype as
float circle(void);
and you will get error.
That's because function
float circle();
declaration doesn't declare function that takes no arguments.
It's implicitly declared as a function that takes undefined number of integer variables as arguments.
Just like
function();
is valid function declaration. Implicitly this function will be treated as function taking int as arguments and returning int.
If you want to declare function function taking no arguments or not returning any value, you do it with void keyword:
void funct(void);

Error in calling function

While compiling this code in the terminal, I am getting an error saying :
newfile1.c:17: error: conflicting types for ‘average’
newfile1.c:2: note: previous declaration of ‘average’ was here
I don't see what is wrong with the code. Could someone help me out?
enter code here
#include<stdio.h>
float average(float);
int main()
{
float marks[4],avg;
int i;
printf("Please enter your marks\n");
for(i=0;i<=3;i++)
{
scanf("%d",&marks[i]);
}
avg = average(marks[4]);
printf("The average marks value is %f",avg);
return 0;
}
float average(float a[4])
{
int i,sum;
float avg_m;
for(i=0;i<=3;i++)
{
sum=sum+a[i];
}
avg_m=sum/3;
return avg_m;
}
Replace
float average(float);
with
float average(float[]);
The function declaration and definition are not matching.
Then call the function like this:
avg = average(marks);
Change line in your file
float average(float);
to
float average(float []);
You have declared the function to take one float instead you want array of floats.
Also, while calling it in main, change to
avg = average(marks);
float average(float);
expects a float variable . You need to pass an array , so add
float average(float[]);. Error happened since your function declaration and definition not matching.
in your main, you should call avg = average(marks); to pass the array to function avg = average(marks[4]); will pass a single variable.
In the prototype of average, you have given float as argument type so compiler is expecting a single float value as argument. If you want to pass an array of values, you have to declare your prototype like this:
float average(float input_marks[]);
You can't give length of an array argument in a prototype or definition. You have to pass array length as a separate argument. So your prototype should look something like
float average(float a[], int a_length);
Your function average takes one float as argument, hence the declaration should be floa avaerage(float). If you do float average(float a[4]) you are telling compiler that your function takes an array of 4 floats as argument.

Resources