隐式函数声明的行为 [英] behaviour of implicit function declaration

查看:175
本文介绍了隐式函数声明的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道使用没有原型的函数是错误的. 但是当我摆弄时,我遇到了这种奇怪冲突的行为.

I know it is wrong to use a function without prototype. But when I was fiddling around, I came across this strange and conflicting behavior.

test1

    #include <stdio.h>
    #include <limits.h>
    void main(){
        char c='\0';
        float f=0.0;
           xof(c,f);/* at this point implicit function declaration is 
generated as int xof(int ,double ); */ 
    }
    int xof(char c,float f)
    {
        printf("%d %f\n", c,f);
    }

隐式函数声明为int xof(int,double);

错误是

variablename.c:8:5:错误:'xof'int xof(char c,f浮动)

variablename.c:8:5: error: conflicting types for 'xof' int xof(char c,float f)

我理解这是因为隐式生成的函数声明(默认将整数值设置为INT,将小数位设置为DOUBLE)与以下函数定义不匹配

I understand this because implicitly generated function declaration (which defaults integer values to INT and decimals to DOUBLE) doesn't match the following function definition

test2

#include <stdio.h>


 #include <limits.h>
    void main(){
        unsigned int a =UINT_MAX;
        int b=0;
        xof(a); /* implicit function declaration should be int xof(int); */
    }

    int xof(unsigned a,int b)
    {
        printf("%d %d\n", a,b);
    }

隐式函数声明为int xof(int); 应该与函数定义冲突

但这可以很好地运行(没有错误),输出为 'a'表现为"int"值,而'b'具有未定义垃圾"

But this runs fine ( no error) and output is with 'a' behaving as 'int' value and 'b' has 'undefined Garbage'

-1 12260176

-1 12260176

有人可以解释这一点. 预先感谢.

Could someone explain this. Thanks in advance.

推荐答案

遇到没有定义的函数调用时,生成的隐式定义将始终为int (*)(),即,函数接受未指定数量的参数并返回int. 考虑函数调用中的实际参数.那就是你的误解的来源.

When a function call is encountered without a definition, the implicit definition generated will always be int (*)(), i.e. a function accepting an unspecified number of arguments and returning int. The actual arguments in the function call are not taken into account. That is where your misconception comes from.

对于第一个程序,生成的错误消息为:

In the case of the first program, the generated error message is:

/tmp/x1.c:10:错误:"xof"的类型冲突
/tmp/x1.c:10: 注意:默认升级的参数类型不能与 空参数名称列表声明
/tmp/x1.c:6:错误:上一个 隐式声明"xof"在这里

/tmp/x1.c:10: error: conflicting types for ‘xof’
/tmp/x1.c:10: note: an argument type that has a default promotion can’t match an empty parameter name list declaration
/tmp/x1.c:6: error: previous implicit declaration of ‘xof’ was here

出现此错误是因为实际的函数定义包含一个或多个参数,这些参数的类型受默认提升规则的约束.具体来说,任何级别低于int(在这种情况下为char)的整数类型都将在表达式中提升为int. float参数也是如此,该参数在表达式中被提升为double.换句话说,不可能通过隐式声明将正确类型的参数传递给该函数.

The error appears because the actual function definition contains one or more parameters whose types are subject to default promotion rules. Specifically, any integer type with a rank lower than int (a char in this case) is promoted to int in an expression. The same goes for the float argument which gets promoted to a double in expressions. In other words, it's impossible to pass parameters of the correct type to this function with an implicit declaration.

第二个程序不会产生错误,因为两个参数(intunsigned int)均不受默认升级规则的约束.在这种情况下,您将调用未定义的行为,因为没有传递正确数量的正确类型的参数.如果您确实传入了2个正确类型的参数,则行为将得到很好的定义.

The second program doesn't generate an error because neither of the arguments (int and unsigned int) are subject to default promotion rules. In this case, you invoke undefined behavior because you aren't passing the proper number of arguments of the correct types. If you did pass in 2 parameters of the correct types, the behavior would be well defined.

请注意,隐式函数声明是C89的一项功能,在C99和更高版本中不受支持,尽管某些编译器仍可以在C99或C11模式下接受它们作为扩展.

Note that implicit function declarations are a C89 feature and are not supported in C99 and later, although some compilers may still accept them in C99 or C11 mode as an extension.

这篇关于隐式函数声明的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆