I'm new to C and i made this program and i'm getting an error "conflicting types for (function)" "previous declaration of (function) was here".
I compiled this using gcc compiler of Dev c++ using command prompt on my system. Can anyone help me understand my fault here ?
#include<stdio.h>
#include<math.h>
main()
{
int a,b,c;
float area;
float ar(int a,int b,int c);
printf("Enter the lenghts of the three sides of a triangle");
scanf("%d%d%d",&a,&b,&c);
area=ar(a,b,c);
printf("The are a of the triangle is=%.2f",area);
}
ar(a,b,c)
{
float area,s;
s=(a+b+c)/3;
area=sqrt((s*(s-a)*(s-b)*(s-c)));
return area;
}
You declared your function "ar" with return type float but float ar(int a,int b,int c); but you defined it with no return value.That rised a problem here. try this:
#include<stdio.h>
#include<math.h>
float ar(int a,int b,int c);
void main()
{
int a,b,c;
float area;
printf("Enter the lenghts of the three sides of a triangle");
scanf("%d%d%d",&a,&b,&c);
area=ar(a,b,c);
printf("The are a of the triangle is=%.2f",area);
}
float ar(int a,int b,int c)
{
float area,s;
s=(a+b+c)/3;
area=sqrt((s*(s-a)*(s-b)*(s-c)));
return area;
}
Compile with the command :gcc -std=c99 -o stack_16_4_16 stack_16_4_16.c -lm
The problem is that you specify a return type in your prototype but not in the actual function definition. When you write the prototype with a particular definition you must follow that same definition when writing the actual function.
I've also cast the integer addition into a float so that you can properly calculate the area. When you take an integer and divide it by a float in C you will end up with an integer regardless of what you are assigning it into. The cast will change this behavior.
Consider this:
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c;
float area;
float ar(int a,int b,int c);
printf("Enter the lenghts of the three sides of a triangle");
scanf("%d%d%d",&a,&b,&c);
area=ar(a,b,c);
printf("The are a of the triangle is=%.2f",area);
}
float ar(int a,int b,int c)
{
float area,s;
s=(float)(a+b+c)/3;
area=sqrt((s*(s-a)*(s-b)*(s-c)));
return area;
}
This will compile cleanly.
I tried to include a bit of explanation here :
#include<stdio.h>
#include<math.h>
float ar(int a,int b,int c);
/* This is a function declaration and just like variables, functions are
* also limited by scope. If you declare the function inside the main,
* then the function cannot be called outside the main. That is the purpose
* it is declared here.
*/
int main()
{
int a,b,c;
float area;
printf("Enter the lengths of the three sides of a triangle : ");
scanf("%d%d%d",&a,&b,&c);
area=ar(a,b,c);
printf("The are a of the triangle is = %.2f",area);
return 0;
}
float ar(int a,int b,int c)
/* Note that I have added the types of arguments
* In older versions of C it is not uncommon to see functions like
* float ar(a,b)
* int a,b;
* {Some Stuff Here}
* The above function style was problematic - it couldn't deal with mismatched arguments.
* So it is a good practice to specify the type of the parameters.
* In fact this was ANSI C standard's solution to the problems of mismatched arguments.
*/
{
float area,s;
s=(a+b+c)/(float)2;
/* Either the numerator or the denominator should be float for the output to be float
* So I casted the denominator to a float value. Also, if you're using Heron's formula
* the denominator should be 2 , not 3.
*/
area=sqrt(s*(s-a)*(s-b)*(s-c));
return area;
}
You should add a return type to your function ar
#include<stdio.h>
#include<math.h>
main()
{
int a,b,c;
float area;
float ar(int a,int b,int c);
printf("Enter the lenghts of the three sides of a triangle");
scanf("%d%d%d",&a,&b,&c);
area=ar(a,b,c);
printf("The are a of the triangle is=%.2f",area);
}
float ar(float a, float b, float c)
{
float area,s;
s=(a+b+c)/3;
area=sqrt((s*(s-a)*(s-b)*(s-c)));
return area;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.