维基百科说:“通过包含函数原型,您通知编译器函数"fac"需要一个整数参数,并使编译器能够捕获这些类型的错误。”
并使用以下内容作为示例:
#include <stdio.h>
/*
* 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("%d\n", 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);
}
但调用函数的函数定义已经包含了原型告诉编译器的所有信息,为什么编译器不能从被调用函数的定义中推导出这些信息,因为它们包含完全相同的语句/信息?
我缺少什么?看起来是徒劳无功的额外工作。
编辑:谢谢各位。我想当然地认为编译器是多次通过的。我对当前像Python这样的语言很宠爱。由于它太旧而需要一些修补才能在单个通道中准确执行某些操作。现在对我来说更明显了。显然,这需要对编译器链接和编译的方式有相当深入的了解。