2014-09-20 21 views
1
// the same error does not happen if i use int instead of float in function. 
#include <stdio.h> 
#include <stdlib.h> 

int main() 

    { 

float a; 
printf("\nEnter any number"); 

scanf("%f",&a); 

float b = sq(a); 

printf("\nthe square of %f is %f",a,b); 

} 

float sq(float x) 

{ 

float y; 

y = x*x; 

return y; 

} 
//error: conflicting type for sq 
+0

使用C99/C11编译器,或是提高警告级别。 – mafso 2014-09-20 18:21:29

回答

4

编译器第一次遇到sq()main()中。那时,编译器无法知道函数的返回类型是什么。编译器(*)所做的是假设sq()返回int。这个假设是不正确的,因为函数返回float。这会导致您看到的错误。

上移sq()出现上述main(),或插入以下prototypemain()

float sq(float x); 

(*)请注意,这仅适用于预C99编译器,因为这种行为在C99被删除。

+0

@mafso:'double'是一个错字,还是我在这里错过了一些微妙之处? – NPE 2014-09-20 18:25:50

+0

@mafso:完成了,谢谢你的建议。 – NPE 2014-09-20 18:33:20

0

valid code

在GCC编译器,我们必须把函数之前它由主函数调用

+1

请张贴您的代码而不是代码图片。 – Nikita 2017-04-16 15:14:28