在 c 中: func(void) 与 func()
当 C 函数不接受任何参数时,是否必须按照语言规则使用“void”参数来声明/定义它? 当参数列表中没有任何内容时,PC-Lint 似乎会出现问题,我想知道这是否是我不知道的语言语法中的某些内容。
编辑:我刚刚发现了一个重复的(back-dupe?它先出现的)问题,C void argument,其中有更多答案和解释。
When a C function does not accept any arguments, does it have to be declared/defined with a "void" parameter by the language rules?
PC-Lint seems to have problems when there's nothing at all in the argument-list, and I was wondering if it's something in the language syntax that I don't know about.
Edit: I just found a duplicate (back-dupe? it came first) question, C void arguments, which has more answers and explanations.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
void 表示该函数不带任何参数。 例如,
这与定义不同,
因为在第二种情况下,编译器不会检查该函数是否真的在没有参数的情况下被调用; 相反,将接受具有任意数量参数的函数调用,而不会发出任何警告(这只是为了与 ANSI 之前的旧式函数定义语法兼容)。
void means the function does not take any parameters. For example,
This is not the same as defining
because in the second case the compiler will not check whether the function is really called with no arguments at all; instead, a function call with arbitrary number of arguments will be accepted without any warnings (this is implemented only for the compatibility with the old-style function definition syntax, pre-ANSI).
C 中的 IIRC func(void) 将声明一个不带参数的函数,而 func() 声明一个带任意数量参数的函数。 我相信后者是来自 ANSI C 之前的工件。
根据 Wikipedia 这里,声明 func() 基本上声明了“没有有关参数的信息”的函数。
IIRC func(void) in C will declare a function that takes no parameters whereas func() declares a function that will take any number of parameters. I believe the latter is an artifact coming from pre-ANSI C.
According to Wikipedia here, the declaration func() does basically declare the function "without information about the parameters".