[英]My gcc compiler gives me an, error that shows as Conflicting types for (function)
我是C語言的新手,我制作了此程序,但出現錯誤“(函數)的類型沖突”“(函數)的先前聲明在這里”。
我在系統上使用命令提示符使用Dev c ++的gcc編譯器對此進行了編譯。 有人可以幫我了解我的錯嗎?
#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;
}
您使用返回類型float聲明了函數“ ar”,但返回了float ar(int a,int b,int c); 但您沒有定義返回值。這在這里引起了問題。 嘗試這個:
#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;
}
使用命令:gcc -std = c99 -o stack_16_4_16 stack_16_4_16.c -lm編譯
問題在於您在原型中指定了返回類型,但未在實際函數定義中指定返回類型。 當使用特定定義編寫原型時,編寫實際函數時必須遵循相同的定義。
我還將整數加法轉換為浮點數,以便可以正確計算面積。 當您使用整數並將其除以C中的浮點數時,無論您將其分配給什么,都將得到一個整數。 演員表將改變這種行為。
考慮一下:
#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;
}
這將干凈地編譯。
我試圖在這里包括一些解釋:
#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;
}
您應該在函數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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.