本文介绍了声明`int foo(void);后``int foo(){}`与`int foo(void){}`的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 注意:这与 func()vs func(void)不同在c99 中,因为此处的问题专门询问在有效的声明之后遵循零参数函数的实现.Note: this is not the same as func() vs func(void) in c99, because the question here specifically asks about the implementation of a zero-argument function following a valid declaration.零参数的实现是否应包含void关键字?具体来说,C标准对以下两个功能的实现有什么要说的吗?请注意, foo1和foo2都被声明为零参数函数;唯一的区别在于实现,而不是声明:Should the implementation of a zero-argument include the void keyword? Specifically, does the C standard have anything to say about the implementation of the following two functions? Note that both foo1 and foo2 are declared as zero-argument functions; the only difference is in the implementation, not in the declaration:#include <stdio.h>int foo1(void); // inform compiler that foo1 and foo2 are zero-args fns.int foo2(void);int main() { printf("%d\n", foo1()); printf("%d\n", foo2()); return 0;}int foo1(void) { return 22; }int foo2() { return 22; }我注意到gcc -Wall -std=c99 -Wpedantic foo.c -o foo可以在没有任何警告或错误的情况下进行编译和执行,但是是否存在违反标准的情况?I note that gcc -Wall -std=c99 -Wpedantic foo.c -o foo compiles and executes without any warnings or errors, but is there any violation of the standard going on?推荐答案您发布的代码正确. int foo2(void);将foo2声明为不带任何参数,并形成一个原型.The code you have posted is correct. int foo2(void); declares foo2 as taking no arguments, and forms a prototype.函数定义必须与此兼容;并且带有空括号的定义与此原型兼容.这在C11 6.7.6.3/15中有详细说明,这是一个很详尽的说明:The function definition must be compatible with this; and a definition with empty parentheses is compatible with this prototype. This is specified in C11 6.7.6.3/15, which is a mouthful:在这一点上有太多文字的原因是C最初仅具有K& R样式函数,然后添加了原型.因此,必须有文字来涵盖K& R样式与原型样式混合的所有可能组合.我的粗体部分开始的部分是指以前用原型声明过函数的情况下,使用K& R样式的函数定义.The reason there's so much text on this point is that C originally only had K&R style functions, and then prototypes were added . So there has to be text to cover all possible combinations of K&R style mixed with prototype style . The section beginning with my bolded part refers to using a K&R-style function definition when the function has previously been declared with a prototype.也很重要:在C11(6.11.6)中,空括号的使用已经过时了.Also relevant: use of empty parentheses is obsolescent in C11 (6.11.6). 这篇关于声明`int foo(void);后``int foo(){}`与`int foo(void){}`的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!