我对 C99 标准中的默认参数提升有疑问。在“C Programming - A Modern Approach, 2nd Edition”一书中,我读到:
Argument Conversions:
[...]
1) The compiler has encountered a prototype prior to the call. [...]
2) The compiler has not encountered a prototype prior to the call. The compiler performs the default argument promotions: (1)
float
arguments are converted todouble
. (2) The integral promotions are performed, causingchar
andshort
arguments to be converted toint
. (In C99, the integer promotions are performed.)
几行进一步显示了一个示例,其中在调用它之前没有函数原型(prototype)或定义。评论如下:
Of course, a much better solution is to provide a prototype for
square
before calling it. In C99, callingsquare
without first providing a declaration or definition of the function is an error.
这两个草句不是互相矛盾吗?我的意思是,如果 C99 禁止在没有事先声明/定义的情况下调用函数,它如何确定那种函数调用中的提升?
最佳答案
不,它们并不矛盾。
声明不一定是原型(prototype):
int f();
声明函数 f
但不是原型(prototype),因为对参数类型一无所知。
int (a)
in a;
{
...
}
是定义,但也不是原型(prototype)。
关于c - C99 标准中的默认参数提升,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12008951/