Default argument promotions in C function calls

﹥>﹥吖頭↗ 提交于 2019-11-25 22:47:36

问题


Setup

I have a few questions about the default argument promotions when calling a function in C. Here\'s section 6.5.2.2 \"Function calls\" Paragraphs 6, 7, and 8 from the C99 standard (pdf) (emphasis added and broken into lists for ease of reading):

Paragraph 6

  1. If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type float are promoted to double. These are called the default argument promotions.
  2. If the number of arguments does not equal the number of parameters, the behavior is undefined.
  3. If the function is defined with a type that includes a prototype, and either the prototype ends with an ellipsis (, ...) or the types of the arguments after promotion are not compatible with the types of the parameters, the behavior is undefined.
  4. If the function is defined with a type that does not include a prototype, and the types of the arguments after promotion are not compatible with those of the parameters after promotion, the behavior is undefined, except for the following cases:
    • one promoted type is a signed integer type, the other promoted type is the corresponding unsigned integer type, and the value is representable in both types;
    • both types are pointers to qualified or unqualified versions of a character type or void.

Paragraph 7

  1. If the expression that denotes the called function has a type that does include a prototype, the arguments are implicitly converted, as if by assignment, to the types of the corresponding parameters, taking the type of each parameter to be the unqualified version of its declared type.
  2. 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.

Paragraph 8

  1. No other conversions are performed implicitly; in particular, the number and types of arguments are not compared with those of the parameters in a function definition that does not include a function prototype declarator.

What I know

  • The default argument promotions are char and short to int/unsigned int and float to double
  • The optional arguments to variadic functions (like printf) are subject to the default argument promotions

For the record, my understanding of a function prototype is this:

void func(int a, char b, float c);  // Function prototype
void func(int a, char b, float c) { /* ... */ }  // Function definition

Question

I\'m having a really hard time groking all of this. Here are some questions I have:

  • Do prototyped and non-prototyped functions\' behavior really differ so much, such as with regard to default promotions and implicit conversions?
  • When do default argument promotions occur? Is it always? Or is it just in special cases (like with variadic functions)? Does it depend on whether a function is prototyped?

回答1:


Upvoted AProgrammer's answer—those are the real goods.

For those of you who are wondering why things are this way: in the dark ages before 1988, there was no such thing as a function prototype in classic "K&R" C, and the default argument promotions were instituted because (a) there were essentially "free", as it costs no more to put a byte in a register than to put a word in a register, and (b) to cut down on potential errors in parameter passing. That second reason never quite cut it, which was why the introduction of function prototypes in ANSI C was the single most important change ever in the C language.

As to when default promotions kick in: default argument promotions are used exactly when the expected type of the argument is unknown, which is to say when there's no prototype or when the argument is variadic.




回答2:


  • (Non variadic) parameters to functions with a prototype are converted to the corresponding type, which can be char, short, float.

  • Parameters to functions without prototype and variadic parameters are subject to default argument promotions.

If you define a function with a prototype and use it without the prototype or vise versa and it has parameters of type char, short or float, you'll probably have a problem at run-time. You'll have the same kind of problems with variadic functions if the promoted type doesn't match what is used when reading the argument list.

Example 1: problem when defining a function with a prototype and using it without.

definition.c

void f(char c)
{
   printf("%c", c);
}

use.c

void f();

int main()
{
   f('x');
}

can fail because an int will be passed and the function expect a char.

Example 2: problem when defining a function without a prototype and using it with one.

definition.c

void f(c)
   char c;
{
   printf("%c", c);
}

(This is kind of definition is very old fashioned)

use.c

void f(char c);

int main()
{
   f('x');
}

can fail because an int is expected but a char will be passed.

Note: you'll remark that all functions from the standard library have types which result from default promotions. So they didn't cause problem during the transition when prototypes were added.




回答3:


Your confusion stems from a very slight misunderstanding of the terminology - both declarations and definitions can include prototypes (or not):

void func(int a, char b, float c);

That is a function declaration that includes a prototype.

void func(int a, char b, float c) { /* ... */ }

That is a function definition that includes a prototype.

"Prototyped" and "non-prototyped" are just attributes of a function type, and both declarations and definitions introduce the type of the function.

So you can have a declaration without a prototype:

void func();

or you can have a definition without a prototype (K&R C style):

void func(a, b, c)
    int a;
    char b;
    float c;
{ /* ... */ }


来源:https://stackoverflow.com/questions/1255775/default-argument-promotions-in-c-function-calls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!