我在编译时遇到以下错误。我知道这听起来不对,但编译器试图传达的确切信息是什么: 错误:“乐趣”的类型冲突 错误:以前的乐趣声明在这里:
int main( )
{
extern int fun(float);
int a;
a=fun(3.14F);
printf("%d\n",a);
return 0;
}
int fun( aa )
float aa;
{
return( (int) aa);
}
最佳答案
K&R 风格的函数声明与现代风格的函数声明不太一样。特别是,发生了默认参数提升,使您的float
参数不太合法。您有两种选择来解决您的问题:
更改
fun
以接受double
参数而不是float
。将
fun
的定义更改为标准 C 风格的函数定义:int fun(float aa) { return aa; }
我还删除了不必要的转换和括号。
顺便说一句,如果您是初学者,您可能会发现 clang有帮助 - 它有时会提供更好的错误消息。对于您的程序,例如:
example.c:13:7: warning: promoted type 'double' of K&R function parameter is not
compatible with the parameter type 'float' declared in a previous
prototype [-Wknr-promoted-parameter]
float aa;
^
example.c:5:25: note: previous declaration is here
extern int fun(float);
^
关于c - 使用 K&R 样式函数定义时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17193395/