微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

以下功能定义如何工作?

#include <stdio.h>
void main() {
    extern int fun(float);
    int a=fun(3.5);
    printf("%d",a);
}

int fun(aa)
float aa;
{
    return ((int)aa);
}

上面提到的代码块可以在Visual Studio 8编译器上编译,尽管输出垃圾值.但是当我在gcc-4.3.4中编译相同的代码时,我收到以下编译错误

prog.c:2: warning: return type of ‘main’ is not ‘int’
prog.c:8: error: conflicting types for ‘fun’
prog.c:3: error: prevIoUs declaration of ‘fun’ was here

当它具有以下属性时它将如何工作:

>在函数体开始之前有一个变量声明.
>函数定义没有变量的参数类型.

解决方法

功能以K&R风格编写,您的原型不正确.其实也有其他的问题呢
#include <stdio.h>
void main() {
    extern int fun(float);
    int a=fun(3.5);
    printf("%d",a);
}

int fun(aa)
float aa;
{
    return ((int)aa);
}

main()的返回类型是int,至少在Standard C中.你的print语句应该包含换行符.

如果函数fun()是用原型编写的,你的原型就可以了:

int fun(float aa) { ... }

但是,该函数是用K&R的方式写的,因此函数希望被传递一个double,它将转换为float:

int fun(double aa_arg) { float aa = aa_arg; ... }

在K& R中,所有浮动值都是双倍.这就是为什么你正在获得垃圾你正在(或许不知不觉地)对你的编译器说谎,并且通过对你进行GIGO来回归自己.

FWIW:GCC 4.6.1拒绝编译代码(即使没有任何警告设置).它抱怨:

f1.c: In function ‘main’:
f1.c:2: warning: return type of ‘main’ is not ‘int’
f1.c: At top level:
f1.c:9: error: conflicting types for ‘fun’
f1.c:3: error: prevIoUs declaration of ‘fun’ was here

您可以通过多种不同的方式解决此问题:

#include <stdio.h>
int main(void)
{
    extern int fun(float);
    int a = fun(3.5);
    printf("%d\n",a);
    return(0);
}

int fun(float aa)
{
    return ((int)aa);
}

要么:

#include <stdio.h>
int main(void)
{
    extern int fun(double);
    int a = fun(3.5);
    printf("%d\n",a);
    return(0);
}

int fun(double aa)
{
    return ((int)aa);
}

要么:

#include <stdio.h>
int main(void)
{
    extern int fun(double);
    int a = fun(3.5);
    printf("%d\n",a);
    return(0);
}

int fun(aa)
double aa;
{
    return ((int)aa);
}

要么:

#include <stdio.h>
int main(void)
{
    extern int fun(double);
    int a = fun(3.5);
    printf("%d\n",a);
    return(0);
}

int fun(aa)
float aa;
{
    return ((int)aa);
}

要么:

#include <stdio.h>
int main(void)
{
    extern int fun();
    int a = fun(3.5);
    printf("%d\n",a);
    return(0);
}

int fun(aa)
float aa;
{
    return ((int)aa);
}

我相信这些都是正确的,他们都应该编译没有警告(除非你要求编译器抱怨老式(K& R)函数定义等).

有了海湾合作委员会,我会得到警告:

/usr/bin/gcc -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-deFinition f2.c -o f2  
f2.c:12: warning: no prevIoUs prototype for ‘fun’
/usr/bin/gcc -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-deFinition f3.c -o f3  
f3.c:12: warning: no prevIoUs prototype for ‘fun’
/usr/bin/gcc -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-deFinition f4.c -o f4  
f4.c:12: warning: function declaration isn’t a prototype
f4.c: In function ‘fun’:
f4.c:13: warning: old-style function deFinition
/usr/bin/gcc -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-deFinition f5.c -o f5  
f5.c:12: warning: function declaration isn’t a prototype
f5.c: In function ‘fun’:
f5.c:13: warning: old-style function deFinition
/usr/bin/gcc -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-deFinition f6.c -o f6  
f6.c: In function ‘main’:
f6.c:5: warning: function declaration isn’t a prototype
f6.c: At top level:
f6.c:12: warning: function declaration isn’t a prototype
f6.c: In function ‘fun’:
f6.c:13: warning: old-style function deFinition

原文地址:https://www.jb51.cc/c/111924.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐