为什么没有参数的函数(与实际函数定义相比)会编译?

本文探讨了在C语言中,函数原型没有参数却能成功编译的原因。C语言中,空参数列表意味着可以接受任意数量的参数,而未指定类型的参数默认为整型(int)。虽然在某些编译器中可以正常工作,但这种做法不推荐,应使用void明确表示无参数。
摘要由CSDN通过智能技术生成

本文翻译自:Why does a function with no parameters (compared to the actual function definition) compile?

I've just come across someone's C code that I'm confused as to why it is compiling. 我刚刚遇到了某人的C代码,但对于为什么编译它感到困惑。 There are two points I don't understand. 我不明白两点。

First, the function prototype has no parameters compared to the actual function definition. 首先,函数原型与实际函数定义相比没有参数。 Second, the parameter in the function definition does not have a type. 其次,函数定义中的参数没有类型。

#include <stdio.h>

int func();

int func(param)
{
    return param;
}

int main()
{
    int bla = func(10);    
    printf("%d", bla);
}

Why does this work? 为什么这样做? I have tested it in a couple of compilers, and it works fine. 我已经在几个编译器中对其进行了测试,并且工作正常。


#1楼

参考:https://stackoom.com/question/wXCM/为什么没有参数的函数-与实际函数定义相比-会编译


#2楼

In C func() means that you can pass any number of arguments. 在C中func()表示您可以传递任意数量的参数。 If you want no arguments then you have to declare as func(void) . 如果不需要参数,则必须声明为func(void) The type you're passing to your function, if not specified defaults to int . 如果未指定,则传递给函数的类型默认为int


#3楼

If the function declaration has no parameters ie empty then it is taking unspecified number of arguments. 如果函数声明没有参数,即为空,那么它将使用未指定数量的参数。 If you want to make it take no arguments then change it to: 如果要使其不带任何参数,则将其更改为:

int func(void);

#4楼

  • The empty parameter list means "any arguments", so the definition isn't wrong. 空的参数列表表示“任何参数”,因此定义没有错。
  • The missing type is assumed to be int . 缺少的类型假定为int

I would consider any build that passes this to be lacking in configured warning/error level though, there's no point in being this allowing for actual code. 我会认为,任何通过此设置的构建都缺少配置的警告/错误级别,因此没有必要考虑实际代码。


#5楼

int func(); is an obsolescent function declaration from the days when there was no C standard, ie the days of K&R C (before 1989, the year the first "ANSI C" standard was published). 是从没有C标准的日子开始的过时函数声明,即K&R C的日子(1989年之前,第一个“ ANSI C”标准发布的年份)。

Remember that there were no prototypes in K&R C and the keyword void was not yet invented. 请记住, K&R C没有原型,关键字void尚未被发明出来。 All you could do was to tell the compiler about the return type of a function. 您所能做的就是告诉编译器函数的返回类型 The empty parameter list in K&R C means "an unspecified but fixed" number of arguments. K&R C中的空参数列表表示“数量未指定但固定”的参数。 Fixed means that you must call the function with the same number of args each time (as opposed to a variadic function like printf , where the number and type can vary for each call). 固定表示每次调用函数时必须使用相同数量的args(这与可变参数函数(如printf )不同,在每次调用中,函数的数量和类型都可能不同。

Many compilers will diagnose this construct; 许多编译器会诊断这种构造。 in particular gcc -Wstrict-prototypes will tell you "function declaration isn't a prototype", which is spot on, because it looks like a prototype (especially if you are poisoned by C++!), but isn't. 特别是gcc -Wstrict-prototypes会告诉您“函数声明不是原型”,因为它看起来像原型(特别是如果您被C ++毒害!),但事实并非如此。 It's an old style K&R C return type declaration. 这是旧式的K&R C返回类型声明。

Rule of thumb: Never leave an empty parameter list declaration empty, use int func(void) to be specific. 经验法则:切勿将空的参数列表声明留​​空,请使用int func(void)进行具体说明。 This turns the K&R return type declaration into a proper C89 prototype. 这会将K&R返回类型声明转换为正确的C89原型。 Compilers are happy, developers are happy, static checkers are happy. 编译器很高兴,开发人员很高兴,静态检查程序很高兴。 Those mislead by^W^Wfond of C++ may cringe, though, because they need to type extra characters when they try to exercise their foreign language skills :-) 但是,那些受C ++的^ W ^ Wong误导的人可能会畏缩,因为在尝试锻炼外语技能时,他们需要键入额外的字符:-)


#6楼

All the other answers are correct, but just for completion 所有其他答案都是正确的,但仅是为了完成

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. return-type是函数返回的变量类型。 This can not be an array type or a function type. 不能是数组类型或函数类型。 If not given, then int is assumed . 如果未给出,则假定为int

function-name is the name of the function. function-name函数的名称

parameter-list is the list of parameters that the function takes separated by commas. parameter-list是函数采用的参数列表,以逗号分隔。 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. 如果未提供任何参数,则该函数将不接受任何参数,并且应使用空括号或关键字void定义该函数。 If no variable type is in front of a variable in the paramater list, then int is assumed . 如果参数列表中的变量之前没有变量类型,则假定为int 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. 注意:使用省略号时,头stdarg.h可用于访问参数。

And again for the sake of completeness. 再次为了完整性。 From C11 specification 6:11:6 (page: 179) 从C11规范6:11:6 (页面:179)

The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature . 使用带空括号的函数声明符 (不是原型格式的参数类型声明符) 是过时的功能

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值