在C语言标准中g(void)表示没有参数,g()对应不确定个数的参数。
在C++中两者都表示没有参数。
// C code
extern int foo(); // Unspecified parameters
extern int bar(void); // No parameters
void baz()
{
foo(0); // Valid C, invalid C++
foo(1, 2); // Valid C, invalid C++
bar(); // Okay in both C and C++
bar(1); // Error in both C and C++
}
// C++ code
extern int xyz();
extern int xyz(void); // Same as 'xyz()' in C++,
// Different and invalid in C
因此为了使程序兼容两种语言的编译器,可以将函数显式地声明void参数。
// Compiles as both C and C++
int bosho(void)
{
...
}
温馨提示:答案为网友推荐,仅供参考