The meanings of the phrases trailing arguments and default argument promotion are unclear to me as used in the following excerpts, where the two paragraphs seem almost contradictory, leading me to be unclear about when default promotions should be expected.
ISO/IEC 9899:201x section 6.5.2.2 Function calls:
- para 6: If the expression that denotes the called function has a type that does not include a prototype,..., and arguments that have type
floatare promoted todouble. These are called the default argument promotions. - para 7: "...The ellipsis notation in a function prototype declarator causes argument type conversion to stop after the last declared parameter. The default argument promotions are performed on trailing arguments."
From paragraph 6, (a type that does not include a prototype) seems to suggest that only arguments 4.0 and 5.0 will undergo default promotions. Then in para. 7 it says promotion stops after the last declared parameter. (I believe that is b). Seeming to suggest that a and b will undergo promotions, but nothing following them in the argument list will be promoted. But then it goes on to say default promotions are performed on trailing arguments. Trailing means at the end of, indicating those allowed by the ellipsis.
So what exactly gets promoted when calling f(), and Why?
int f(float a, float b, ...); int main(void) { float a = 1.0; float b = 2.0; int res = f(a, b, 4.0, 5.0); return 0; } int f(float a, float b, ...) { ... }
4.0and5.0are both of typedoubleand undergo no promotion. Paragraph 7 says that the normal conversion of actual argument type to the type demanded by the prototype stops when there are no more named parameters because the arguments are passed 'to the ellipsis' part of the function signature. If you passed1.0in place ofa, then thedoublevalue would be converted tofloatbecause of the prototype (assumingfwas declared correctly before it was called). If you passed4.0Finstead of4.0, thefloatvalue would be default promoted todouble.sqrt(2)to work correctly — converting the integer argument todoublebecause of the prototype. Default argument promotions occur when there is no prototype or when the argument is passed 'to the ellipsis' part of a prototyped function.