Getting Previous declaration is here Error in C - 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.

Related

Function declaration before and after main()

#include <stdio.h>
int add2nums( int, int);
void main(void)
{
int y,a,b;
printf("Enter 2 numbers\n");
scanf("%d%d", &a, &b);
y = add2nums(a,b);
printf("a is %d\n", a);
printf("b is %d\n", b);
printf("y is %d\n", y);
}
int add2nums( int num1, int num2)
{
int sum;
sum = num1 + num2;
return(sum);
}
So usually, when I create new functions in C the definition of the function is created before the main() function.
In my lecture, there is this example of how to create a function prototype and how they are created by declaring it before the main() function and then defining it after the end of the main() function.
When running the program above, the following error comes up:
Line5: warning: return type of 'main' is not 'int' [-Wmain]|
What is happening? And why is the declaration of the function add2nums() occur twice once before main() and with no parameters?
int add2nums( int, int);
and then again after the end of main() with parameters num1 and num2
int add2nums( int num1, int num2)
There are two valid signatures for the main() function:
int main( void )
int main( int argc, char *argv[] )
Notice that both valid signatures have a return type of int. Any other return type, such as void is not valid and causes the compiler to output a warning message.
When the code calls a function, the compiler needs to know the signature of that called function. There are two ways to tell the compiler what the signature of the called function is:
have the whole function listed before where it is called
have a prototype (aka forward reference) of the function signature before where the function is called. In a prototype the compiler only needs the returned type and the types of the parameters. However, it is a good programming practice to have the names of the parameters listed in the prototype as a courtesy to the humans reading the code.
In C language, the main function has a signature int main(), which lets you to return a value back to the OS, but you made the main() with no void return type. Because of that you've got the warning.
int add2nums( int, int); is just a declaration of the function which lets the compiler make a reference to the function. That's because the compiler reads the file only once.
Names of the parameters aren't required, because they aren't part of the signature.
Declaration of the function is the information for the compiler (not linker as another answer states) - what type the parameters of the function are and what type of the return value is.
So the names of the parameters are not needed.

I'm using the following C code [duplicate]

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.

not possible to pass a float to another function?

I want to pass a float number to another function.
int main()
{
float start=0;
float step=0.1;
int number=10;
fun(start,step,number)
}
fun(float star, float ste, int numbe)
{
//here I get "star = 0", "numbe = 10", but "ste = -1.084264e-19"
}
what is wrong here?
Thank you
It is most certainly possible to pass a float to a function, as long as you provide a prototype or move the definition ahead of the first use of the function.
A prototype (also called a forward declaration) looks like this:
void fun(float star, float ste, int number);
In larger projects, prototypes go into header files.
Note: do not forget to add void in front of the function definition as well. Otherwise, the compiler treats your function as returning an int.
If you omit the prototype, the compiler will default to using the old K&R C rules to decide how to pass arguments to your function. The result is the unusual behaviour you are seeing, and hopefully a compiler warning as well.
You need a correct function declaration before your function call:
Add:
void fun(float star, float ste, int numbe);
before your main declaration and also add void return type in your fun function definition.
you need to declare the function before main function.
Try this:
void fun(float star, float ste, int number);
int main()
{
float start=0;
float step=0.1;
int number=10;
fun(start,step,number);
}

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