我正在阅读一本关于 C 代码混淆的旧书(该书于 1993 年打印),我注意到带参数的函数是这样实现的:
real_dump(address, infunc, ofp)
char *address;
int (*infunc)();
FILE *ofp;
{
/* the code goes here... */
}
此外,没有定义返回类型。
这是一个旧标准吗?是否可以启用 gcc 来编译此代码?
最佳答案
非原型(prototype)形式的函数定义是有效的 C89、C99 和 C11 代码。
它被称为旧式函数定义,但此功能自 C89 起被标记为过时功能。
这种形式应该不用于新程序。
C99 基本原理说:
"Characterizing the old style as obsolescent is meant to discourage its use and to serve as a strong endorsement by the Committee of the new style."
甚至 K&R2 也不鼓励使用它:
"The old style of declaration and definition still works with ANSI C, at least for a transition period, but we strongly recommend that you use the new form when you have a compiler that supports it."
现在您的函数也没有返回类型,并且自 C99 以来,在函数声明或函数定义中省略返回类型不再有效。在 C99 之前,没有返回类型的函数隐式返回一个 int
。
关于gcc
的问题,gcc
默认使用-std=gnu89
编译。这意味着 C89 标准 + gcc 扩展。所以默认情况下,gcc
将接受使用旧式形式且没有返回类型的函数声明和定义来编译程序。
关于c - 在 ")"之后定义函数的参数类型,这是一个非常古老的标准吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8869768/