我刚刚遇到了一些人的C代码,我很困惑为什么它要编译。有两点我不明白。
与实际函数定义相比,函数原型没有参数。 函数定义中的形参没有类型。
#include <stdio.h>
int func();
int func(param)
{
return param;
}
int main()
{
int bla = func(10);
printf("%d", bla);
}
为什么会这样? 我已经在几个编译器中测试了它,它工作得很好。
我刚刚遇到了一些人的C代码,我很困惑为什么它要编译。有两点我不明白。
与实际函数定义相比,函数原型没有参数。 函数定义中的形参没有类型。
#include <stdio.h>
int func();
int func(param)
{
return param;
}
int main()
{
int bla = func(10);
printf("%d", bla);
}
为什么会这样? 我已经在几个编译器中测试了它,它工作得很好。
当前回答
如果函数声明没有形参,即为空,则它将接受不指定数量的参数。如果你想让它不带参数,那么将其更改为:
int func(void);
其他回答
在C语言中,func()意味着你可以传递任意数量的参数。如果你不想要参数,那么你必须声明为func(void)。传递给函数的类型,如果没有指定,默认为int。
它是K&R风格的函数声明和定义。来自C99标准(ISO/IEC 9899:TC3)
函数声明器(包括原型)
标识符列表只声明函数参数的标识符。一个空 作为该函数定义的一部分的函数声明器中的列表指定 函数没有参数。类的一部分,函数声明器中的空列表 类的数量或类型的信息 提供参数。(如果两个函数类型都是“旧样式”,则不比较参数类型。)
函数声明器
使用带有空括号的函数声明符(而不是prototype-format参数) 类型声明器)是一个过时的特性。
函数定义
使用带有单独参数标识符和声明列表的函数定义 (而不是prototype-format参数类型和标识符声明器)是一个过时的特性。
旧的风格是指K&R风格吗
例子:
声明:int old_style();
定义:
int old_style(a, b)
int a;
int b;
{
/* something to do */
}
这就是为什么我通常建议人们使用以下方法编译代码:
cc -Wmissing-variable-declarations -Wstrict-variable-declarations -Wold-style-definition
这些标志执行了一些事情:
-Wmissing-variable-declarations: It is impossible to declare a non-static function without getting a prototype first. This makes it more likely that a prototype in a header file matches with the actual definition. Alternatively, it enforces that you add the static keyword to functions that don't need to be visible publicly. -Wstrict-variable-declarations: The prototype must properly list the arguments. -Wold-style-definition: The function definition itself must also properly list the arguments.
在许多开源项目中也默认使用这些标志。例如,FreeBSD在Makefile中使用warnings =6构建时启用了这些标志。
如果函数声明没有形参,即为空,则它将接受不指定数量的参数。如果你想让它不带参数,那么将其更改为:
int func(void);
其他答案都是正确的,但只是为了完成
A function is declared in the following manner: return-type function-name(parameter-list,...) { body... } return-type is the variable type that the function returns. This can not be an array type or a function type. If not given, then int is assumed. function-name is the name of the function. parameter-list is the list of parameters that the function takes separated by commas. If no parameters are given, then the function does not take any and should be defined with an empty set of parenthesis or with the keyword void. If no variable type is in front of a variable in the paramater list, then int is assumed. Arrays and functions are not passed to functions, but are automatically converted to pointers. If the list is terminated with an ellipsis (,...), then there is no set number of parameters. Note: the header stdarg.h can be used to access arguments when using an ellipsis.
为了完整起见。来自C11规范6:11:6(第179页)
使用带有空括号的函数声明器(不是 原型格式参数类型声明器)是过时的 特性。