default argument promotions in the case of inplici

2019-06-13 19:06发布

问题:

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?

回答1:

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).