Declaring parameters outside the declarator

Standard C states that to define a function, if the declarator includes a list of identifiers, parameter types must be declared in the following declaration list. Apparently that matters.

extern int max(int a, int b) { return a > b ? a : b; } extern int max(a, b) int a, b; { return a > b ? a : b; } 

Here int a, b; This is a list of declarations for parameters. the difference between these two definitions is that the first form acts as a prototype declaration, which forcibly converts the arguments to subsequent function calls, while the second form does not.

What does this mean for the programmer and does it affect the code generated by the compiler?

+7
source share
1 answer

This means that in the second case, the caller's responsibility is to ensure that the arguments provided are of the correct type; Implicit conversion will not be implied (except for promotions by default). From section 6.5.2.2:

If the expression denoting the called function is of a type that does not include the prototype, entire promotions are executed for each argument.

...

If the expression that denotes the called function is of a type that includes the prototype, the arguments are implicitly converted, as if they were assigned, the types have the appropriate parameters.

So caller code like this will be fine:

 char x = 3; char y = 7; max(x, y); // Equivalent to max((int)x, (int)y) 

because x and y pushed to int before being pushed onto the stack.

However, code like this will be out of order:

 double x = 3.0; long y = 7; max(x, y); // Uh-oh 

x and y will be pushed onto the stack as double and long , but max() will try to read two int s, which will lead to undefined behavior (in practice, the original bits will be reinterpreted).

This is one reason not to use the second form; the only reason the standard is backward compatible with (extremely) legacy code. If you use GCC, you can apply this using the -Wold-style-definition flag; I hope other compilers would suggest something equivalent.

+11
source

All Articles