加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

以下功能定义如何工作?

发布时间:2020-12-16 03:18:51 所属栏目:百科 来源:网络整理
导读:#include stdio.hvoid 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中编译相同的代码时,我收到以
#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("%dn",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("%dn",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("%dn",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("%dn",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("%dn",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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读