C函数前向声明省略参数
这样的不带参数的函数声明,在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:
C函数前向声明省略参数的更多相关文章
- 类中函数前、后、参数加const
1.参数加const:int fun(const int a) a在函数里不可被修改 2.函数前加const:const int* const fun() 这种一般是返回的指针或者是引用,加const ...
- 详解Python函数参数定义及传参(必备参数、关键字参数、默认可省略参数、可变不定长参数、*args、**kwargs)
详解Python函数参数定义及传参(必备参数.关键字参数.默认可省略参数.可变不定长参数.*args.**kwargs) Python函数参数传参的种类 Python中函数参数定义及调用函数时传参 ...
- 关于C#中函数声明带参数的函数
在C#语言的函数中,有一项至关重要的我们称之为参数. 对于参数的含义:要完成一件事,需要知道的额外条件 其语法: static void 函数名(参数列表){ //注释类容} 而其参数列表的语法为: ...
- C++命名空间、函数重载、缺省参数、内联函数、引用
一 .C++入门 1.C++关键字 2.命名空间 3.C++输入&输出 4.缺省参数 5.函数重载 6.引用 7.内联函数 8.auto关键字 9.基于范围的for循环 10.指针空值null ...
- C++ 函数 函数的重载 有默认参数的函数
函数的重载 C++允许用同一函数名定义多个函数,这些函数的参数个数和参数类型不同.这就是函数的重载(function overloading). int max1(int a,int b, int c ...
- js函数前加分号和感叹号的作用
js函数前加分号和感叹号是什么意思?有什么用? 一般看JQuery插件里的写法是这样的 (function($) { //... })(jQuery); 今天看到bootstrap的javascrip ...
- 类声明、类作用域、前向声明、this指针、嵌套类、PIMPL 技法 等
一.类声明 //类是一种用户自定义类型,声明形式: class 类名称 { public: 公有成员(外部接口) private: 私有 ...
- 【VS开发】【C++开发】const在函数前与函数后的区别
const在函数前与函数后的区别 一 const基础 如果const关键字不涉及到指针,我们很好理解,下面是涉及到指针的情况: int b = ...
- Python - 函数形参之必填参数、缺省参数、可变参数、关键字参数的详细使用
Python函数形参 必传参数:平时最常用的,必传确定数量的参数 缺省参数:在调用函数时可以传也可以不传,如果不传将使用默认值 可变参数:可变长度参数 关键字参数:长度可变,但是需要以kv对形式传参 ...
随机推荐
- 评论模块Demo(XML读写,定时器。)
这个Demo主要是自己做练习熟悉jquery,ajax,与xml文件的读写,以下是实现页面效果: 后台控制器: public ActionResult AddMsg() { XmlDocument x ...
- 【EF】EF实现大批量数据库插入操作
通过参考http://www.itnose.net/news/171/6306259里面介绍的方法,我们可以在程序的解决方案的引用中选择NuGet管理包中添加 该扩展方法提供BulkInsert() ...
- BZOJ1195 HNOI2006最短母串(状压dp)
按照子串出现的先后考虑.令f[i][j]为已经出现的字符串集合为i,最后一个出现的字符串为j时的最短串长,预处理一下任意两个串的最长重叠长度,转移显然.有点麻烦的是字典序,强行增加代码难度. 另一个比 ...
- P1278 单词游戏
题目描述 Io和Ao在玩一个单词游戏. 他们轮流说出一个仅包含元音字母的单词,并且后一个单词的第一个字母必须与前一个单词的最后一个字母一致. 游戏可以从任何一个单词开始. 任何单词禁止说两遍,游戏中只 ...
- [转载]看看大牛们是怎样获得英文综述(reviews)的?
对于做实验的童鞋来说,平时看综述必不可少,可是如何获得自己想要的却并不是一件容易的事情,这里整理并推荐几种行之有效的方法: 1. http://www.annualreviews.org/,这是一个顶 ...
- POJ2942:Knights of the Round Table——题解
http://poj.org/problem?id=2942 所写的tarjan练习题最难的一道. 说白了难在考得不是纯tarjan. 首先我们把仇恨关系处理成非仇恨关系的图,然后找双连通分量,在双连 ...
- BZOJ2395:[Balkan 2011]Timeismoney——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=2395 有n个城市(编号从0..n-1),m条公路(双向的),从中选择n-1条边,使得任意的两个城市 ...
- CC TSUBSTR:Substrings on a Tree——题解
https://www.codechef.com/problems/TSUBSTR https://vjudge.net/problem/CodeChef-TSUBSTR 给一棵点权为字母的树,你只能 ...
- LeetCode中二叉树题目总结
本文仅为博主个人总结,水平有限,欢迎大神指出不妥处. 关于二叉树的相关概念可以参见二叉树的百度百科,或binary tree Wiki. 二叉树结点类的常见定义为: /* Definition for ...
- BZOJ1041 [HAOI2008]圆上的整点 【数学】
1041: [HAOI2008]圆上的整点 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 4631 Solved: 2087 [Submit][S ...