0

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
abc
  • 11,579
  • 2
  • 26
  • 51

1 Answers1

3

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

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • thank you my doubt comes because if I call f passing a float, that is promoted to double, and f has an argument of type float I have an error compiling the code, but in the 2 cases I have no errors – abc Apr 27 '12 at 20:24
  • Are you sure these programs have undefined behavior? Last time I read the standard it said something which sounded to me like, that if you call a function `int f(int a)` with a `long` argument (say `x`), that this will happen as if one assignes `int a = x;`. And assignement will implicitly convert the `long` type to `int`. Or am I wrong? – Anthales Apr 27 '12 at 20:29
  • Ah, sorry, I get it, it's about the implicit declaration - if it is implicitly declared as `int f(long a)` but (externally) defined `int f(int a)`, yeah, this will be undefined. – Anthales Apr 27 '12 at 20:43
  • It's still strange, that both programs compile without errors though (at least on gcc). If the compiler sees the implicit declaration (which it does) and the definition afterswards, it should (or even _must_?) throw an error because of mismatching parameter types. – Anthales Apr 27 '12 at 20:54