在隐式函数声明的情况下默认参数提升 [英] default argument promotions in the case of inplicit function declarations

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

问题描述

我试图在老问题中搜索,但我还没有解决我的问题。



我尝试解释我的疑问;
假设在c89模式下工作,如果在函数调用之前没有函数的原型,那么函数有一个隐式声明,函数的类型是 int 和参数通过默认参数促销进行转换:

lockquote

类型为char或short int的对象(不论是否有符号)是
酌情提升为int或unsigned int;并且
类型的float类型对象被提升为double类型。

所以,如果我编写这样的代码, m同意它必须工作:

  int main(void){
char a;
short b,c;
f(a,b,c);
返回0;
}

int f(int a,int b,int c){
return 1;

$ / code>

同样在这里:

  int main(void){
float a;
short b,c;
f(a,b,c);
返回0;
}

int f(double a,int b,int c){
return 1;
}

但是我不明白为什么以下两种情况会起作用

  / * a)* / 
int main(void){
short a,b;
float c;
f(a,b,c);
返回0;
}

int f(长a,长b,长c){
return 1;
}

/ * b)* /

int main(void){
long a,b,c;
f(a,b,c);
返回0;
}

int f(int a,double b,double c){
return 1;
}

在a):
a和b被提升为int ,c要加倍,然后呢?在这种情况下,b):
这里没有DAP会发生什么?


$ b $所以问题是:在DAP之后或DAP未执行时,参数的类型不是相同类型的参数,在隐式函数声明的情况下应用了什么规则?

解决方案

在这两种情况下,都没有规则。这两个程序都表现出未定义的行为。



具体而言,在程序b中,DAP不适用于传递给 f 类型为 long 。只有 char 浮动 p>

要查看可能会发生什么,请将值赋给 a b c main 中,然后尝试打印这些(编程程序b )。

I've tried to search in old questions but I've not solved my problem.

I try to explain my doubt; Supposing to work in c89 mode, if there's not a prototype of the function before the function call, there is an implicit declaration of the function, the type of the function is int and the arguments are converted through Default Argument Promotions:

the objects of type char or short int (whether signed or not) are promoted to either int or unsigned int, as appropriate; and that objects of type float are promoted to type double.

So, if I write a code like this I'm agree that it has to work:

int main(void){
    char a;
    short b,c;
    f(a,b,c);
    return 0;
}

int f(int a,int b,int c){
    return 1;
}

The same here:

int main(void){
    float a;
    short b,c;
    f(a,b,c);
    return 0;
}

int f(double a,int b,int c){
    return 1;
}

But I don't understand why the following 2 cases work

/*a)*/
int main(void){
    short a,b;
    float c;
    f(a,b,c);
    return 0;
}

int f(long a,long b,long c){
    return 1;
}

/*b)*/

int main(void){
    long a,b,c;
    f(a,b,c);
    return 0;
}

int f(int a,double b,double c){
    return 1;
}

in the case a): a and b are promoted to int,c to double and then?

in the case b): there's not DAP here what happens?

So the question is: When, after the DAP or when the DAP is not performed the type of the arguments are not of the same type of the parameters, what rule is applied in the case of implicit function declarations?

解决方案

In both cases, there's no rule. Both programs exhibit undefined behavior.

Specifically, in program b, DAP does not apply as the values passed to f are of type long. Only char, short and float are promoted.

To see what might happen, give values to a, b and c in main and try printing those (program a, program b).

这篇关于在隐式函数声明的情况下默认参数提升的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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