我在 C/C++ Prototype声明中阅读维基百科,我感到困惑:
维基百科说:"通过包含函数 Prototype,你可以通知编译器函数"变量takes一个整数参数,你可以让编译器捕捉这些错误。"
并使用下面的示例:#include
/*
* If this prototype is provided, the compiler will catch the error
* in main(). If it is omitted, then the error will go unnoticed.
*/
int fac(int n);/* Prototype */
int main(void) {/* Calling function */
printf("%dn", fac());/* ERROR: fac is missing an argument! */
return 0;
}
int fac(int n) {/* Called function */
if (n == 0)
return 1;
else
return n * fac(n - 1);
}
但被调用函数的函数定义已经包括所有原型的信息告诉编译器,那么为什么不能编译器推断这个信息从被调用的函数的定义,因为他们包含相同语句/信息信信?
我错过了什么似乎额外的工作,没有明显的增益。
:谢谢伙计们,我猜想编译器是 multi-pass 。 我被 python 这样的语言 spoiled 。 它很有意义,因为它太旧了,需要一些kludges来在单一的处理过程中精确地完成事情。 现在看来更明显了。 显然,它需要对编译器链接和编译的方式有相当的了解。