本文翻译自: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 . 使用带空括号的函数声明符 (不是原型格式的参数类型声明符) 是过时的功能

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

  1. C++ 笔记(13)— 函数(函数声明、函数定义、函数调用[传值、指针、引用]、函数参数默认值、函数重载)

    每个 C++ 程序都至少有一个函数,即主函数 main() ,所有简单的程序都可以定义其他额外的函数. 1. 函数声明 函数声明告诉编译器函数的名称.返回类型和参数.函数声明包括以下几个部分: ret ...

  2. R语言使用plot函数和lines函数可视化线图(line plot)时、图之间的主要区别是由选项type产生的、type参数常用参数说明、不同type生成的可视化图像对比

    R语言使用plot函数和lines函数可视化线图(line plot)时.图之间的主要区别是由选项type产生的.type参数常用参数说明.不同type生成的可视化图像对比 目录

  3. c语言函数参数类型检查,内联函数在编译时是否做参数类型检查?

    先说宏和函数的区别: 1. 宏做的是简单的字符串替换(注意是字符串的替换,不是其他类型参数的替换),而函数的参数的传递,参数是有数据类型的,可以是各种各样的类型. 2. 宏的参数替换是不经计算而直接处 ...

  4. matlab 函数 向量参数,Scipy integrate(quad,quadration,nquad)不能集成向量参数化函数?等效函数(MATLAB works)...

    我正在将一些代码从MATLAB转换为python,并且正在努力获得一个函数,该函数以数组参数(作为参数)来使用Scipy进行集成.在 我已经将代码简化为一个在Scipy中产生相同错误的基本示例,而等效 ...

  5. JNI调用c++函数,该函数的参数是结构体(——对象的传递)

    第三方C++函数接口为 int api_get_logfile(Struct fileinfo tfile),参数是个结构体,且套了另一个结构体: struct fileinfo{  char *fu ...

  6. C++基础8【难】 回顾:数组指针,函数指针,函数指针做函数参数 C语言多态

    1,数组指针语法梳理 回顾,如何定义数组数据类型: 回顾,如何定义指针类型数组: 回顾,如何直接定义 一个指向数组类型的指针: 2,函数指针语法梳理 1)如何定义一个函数类型 2)如何定义一个函数指针 ...

  7. Python函数01/函数的初识/函数的定义/函数调用/函数的返回值/函数的参数

    Python函数01/函数的初识/函数的定义/函数调用/函数的返回值/函数的参数 内容大纲 1.函数的初识 2.函数的定义 3.函数的调用 4.函数的返回值 5.函数的参数 1.函数初识 # def ...

  8. [Microsoft][ODBC SQL Server Driver][SQl Server]参数数据类型 text 对于 replace 函数的参数 1 无效。...

    说到数据库中文本的替换,一般人首先想到的是Replace函数: update Table set Column=Replace(Column,'oldkeyword','newkeyword') 不过 ...

  9. 【Kotlin】函数类型 ( 函数类型 | 带参数名称的参数列表 | 可空函数类型 | 复杂函数类型 | 带接收者函数类型 | 函数类型别名 | 函数类型实例化 | 函数调用 )

    文章目录 I . 函数类型 II . 带参数名的参数列表 III . 可空函数类型 IV . 复杂函数类型解读 V . 函数类型别名 VI . 带 接收者类型 的函数类型 VII . 函数类型实例化 ...

最新文章

  1. iclr 2020 | Geom-GCN:几何图神经网络
  2. 当前主要使用的python版本_如何获取当前使用的Python版本信息?(代码示例)
  3. 【100题】第十五题(树的镜像问题)
  4. Eclipse 导入项目有红感叹号的解决方法
  5. Unix 网络编程(四)- 典型TCP客服服务器程序开发实例及基本套接字API介绍
  6. 开发指南专题九:JEECG微云快速开发平台-表单校验组件ValidForm
  7. 用MATLAB玩转机器人--第六章 用MATLAB玩转单关节机器人
  8. overleaf入门
  9. springboot集成oauth2和JWT(授权码模式)
  10. linux下NFS、FTP使用讲解
  11. Perl获取前后任意月份月末
  12. vue 中如何引入字体(思源黑体)
  13. 软件的发展历史(时间轴)
  14. java如何设置成中文字体,Java程序中文字体配置
  15. 机器学习中的数学——常用概率分布(十一):狄利克雷分布(Dirichlet分布)
  16. python 控制手机摄像头_用Python获取摄像头并实时控制人脸 !
  17. Linux中定位JAVAHOME
  18. Vivado® ML 版,让设计更智能化
  19. 智慧工地:“千里眼”视频远程监控系统案例分析
  20. html页面中加skype,添加Skype的按钮,HTML面板

热门文章

  1. 【安卓】【Dimens】适配文件创建
  2. mybatis if-else(chose when otherwise )
  3. iOS比较常用的第三方框架
  4. 怎样退出计算机安全模式,如何退出?进入安全模式之后如何安全的退出啊 – 手机爱问...
  5. matlab 反激实例(S440_Flyback.slx)
  6. 股票入门基础知识4:什么是股市指数?
  7. 腾讯优图NCNN详细分析及实践操作(含Yolov5实践)
  8. 特征缩放+无量纲化:最小最大缩放 - Python代码实现
  9. 人工智能会否让人类失业?新职业“指令师”即将诞生
  10. c语言排序算法插入法,C语言中冒泡法、选择法、插入法三种常见排序算法分析.doc...