这样的不带参数的函数声明,在c中是合法的,表示任意参数;当然我们自己写代码最好不要这样写了,但是读老代码还是会遇到;

 #include <stdio.h>

 void fun();

 int main()
{
fun();
return ;
} void fun(int a)
{
printf("%d\n", a);
}

下面贴一下函数声明的说明:

int func(); is an obsolescent function declaration from the days when there was no C standard, i.e. the days of K&R C (before 1989, the year the first "ANSI C" standard was published).

Remember that there were no prototypes in K&R C and the keyword void was not yet invented. 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. 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).

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. It's an old style K&R C return type declaration.

Rule of thumb: Never leave an empty parameter list declaration empty, use int func(void) to be specific. This turns the K&R return type declaration into a proper C89 prototype. 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 :-)

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. 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.

And again for the sake of completeness. From C11 specification 6:11:6 (page: 179)

The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.

stackoverflow:

http://stackoverflow.com/questions/13950642/why-does-a-function-with-no-parameters-compared-to-the-actual-function-definiti

C函数前向声明省略参数的更多相关文章

  1. 类中函数前、后、参数加const

    1.参数加const:int fun(const int a) a在函数里不可被修改 2.函数前加const:const int* const fun() 这种一般是返回的指针或者是引用,加const ...

  2. 详解Python函数参数定义及传参(必备参数、关键字参数、默认可省略参数、可变不定长参数、*args、**kwargs)

    详解Python函数参数定义及传参(必备参数.关键字参数.默认可省略参数.可变不定长参数.*args.**kwargs) Python函数参数传参的种类   Python中函数参数定义及调用函数时传参 ...

  3. 关于C#中函数声明带参数的函数

    在C#语言的函数中,有一项至关重要的我们称之为参数. 对于参数的含义:要完成一件事,需要知道的额外条件 其语法: static void 函数名(参数列表){ //注释类容} 而其参数列表的语法为: ...

  4. C++命名空间、函数重载、缺省参数、内联函数、引用

    一 .C++入门 1.C++关键字 2.命名空间 3.C++输入&输出 4.缺省参数 5.函数重载 6.引用 7.内联函数 8.auto关键字 9.基于范围的for循环 10.指针空值null ...

  5. C++ 函数 函数的重载 有默认参数的函数

    函数的重载 C++允许用同一函数名定义多个函数,这些函数的参数个数和参数类型不同.这就是函数的重载(function overloading). int max1(int a,int b, int c ...

  6. js函数前加分号和感叹号的作用

    js函数前加分号和感叹号是什么意思?有什么用? 一般看JQuery插件里的写法是这样的 (function($) { //... })(jQuery); 今天看到bootstrap的javascrip ...

  7. 类声明、类作用域、前向声明、this指针、嵌套类、PIMPL 技法 等

    一.类声明 //类是一种用户自定义类型,声明形式: class 类名称 {    public:              公有成员(外部接口)    private:              私有 ...

  8. 【VS开发】【C++开发】const在函数前与函数后的区别

    const在函数前与函数后的区别 一   const基础           如果const关键字不涉及到指针,我们很好理解,下面是涉及到指针的情况:           int   b   =   ...

  9. Python - 函数形参之必填参数、缺省参数、可变参数、关键字参数的详细使用

    Python函数形参 必传参数:平时最常用的,必传确定数量的参数 缺省参数:在调用函数时可以传也可以不传,如果不传将使用默认值 可变参数:可变长度参数 关键字参数:长度可变,但是需要以kv对形式传参 ...

随机推荐

  1. Redis架构演变与redis-cluster群集读写方案

    导言 redis-cluster是近年来redis架构不断改进中的相对较好的redis高可用方案.本文涉及到近年来redis多实例架构的演变过程,包括普通主从架构(Master.slave可进行写读分 ...

  2. CCS3 动画-鼠标放上去放大背景图片

    ---〉 效果如上,一个简单的过渡放大效果, <!DOCTYPE HTML> <html> <body> <style> #test{ width:30 ...

  3. BZOJ4736 温暖会指引我们前行(LCT+最大生成树)

    类似于瓶颈路,满足条件的路径一定在温度的最大生成树上,那么就是一个LCT维护MST的裸题了. #include<iostream> #include<cstdio> #incl ...

  4. wsgiref 源码解析

    Web Server Gateway Interface(wsgi),即Web服务器网关接口,是Web服务器软件和用Python编写的Web应用程序之间的标准接口. 想了解更多关于WSGI请前往: h ...

  5. html的body内标签之label标签和fieldset标签

    1. <label> 标签为 input 元素定义标注(标记). label 元素不会向用户呈现任何特殊效果.不过,它为鼠标用户改进了可用性.如果您在 label 元素内点击文本,就会触发 ...

  6. Android 打开照相机、获取相册图片、获取图片并裁减

    一.调用照相机 注:surfaceView在当Activity不在前台的时候,会被销毁(onPause方法之后,执行销毁方法)当Activity回到前台时,在Activity执行onResume方法之 ...

  7. POJ3690:Constellations——题解

    http://poj.org/problem?id=3690 题目大意:给一个图和几个子图,判断有多少种子图在原图出现过. —————————————————————— 二维哈希即可,操作看代码,我觉 ...

  8. BZOJ4144 [AMPPZ2014]Petrol 【最短路 + 最小生成树】

    题目链接 BZOJ4144 题解 这题好妙啊,,orz 假设我们在一个非加油站点,那么我们一定是从加油站过来的,我们剩余的油至少要减去这段距离 如果我们在一个非加油站点,如果我们到达不了任意加油站点, ...

  9. JS获取移动端系统信息(操作系统、操作系统版本、横竖屏状态、设备类型、网络状态、生成浏览器指纹)

    function getOS() { // 获取当前操作系统 var os; if (navigator.userAgent.indexOf('Android') > -1 || navigat ...

  10. Linux环境下用Weblogic发布项目【二】 -- 配置Domain域

    配置注意事项: 修改密码时密码长度最少8位:在"<下一步>"后面为空即表示敲回车: 具体配置步骤如下: [root@GPS-App ~]# [root@GPS-App ...