c - 使用 K&R 样式函数定义时出错

标签 c compiler-errors kernighan-and-ritchie

我在编译时遇到以下错误。我知道这听起来不对,但编译器试图传达的确切信息是什么: 错误:“乐趣”的类型冲突 错误:以前的乐趣声明在这里:

 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 参数不太合法。您有两种选择来解决您的问题:

  1. 更改 fun 以接受 double 参数而不是 float

  2. 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/

相关文章:

c - C 中的 double - 打印 50 位有效数字会产生不准确的值

c - 在 Linux 上使用 gcc 链接共享库

c - 如何在c中表示键(enter,shift,alt,space等)

objective-c - prefix 属性后面必须跟一个接口(interface)或协议(protocol)栈溢出

c - k&r 中的“entab”锻炼计划

c - 在C中生成从-n到n的随机数

objective-c - 我应该在头文件中添加一些内容吗?

c - "collect2: error: ld returned 1 exit status"是什么意思?

声明数组的说明

c - stdin、stdout 和 stderr 到标准输入和标准输出的实际分配