
有时我们会看到有两种类型的主函数定义。int main()和int main(void)。那么它们有什么区别吗?
在C++中,它们没有区别。在C中,两者都是正确的。但第二种写法在技术上更好。它指定了函数不接受任何参数。在C中,如果某个函数没有指定参数,那么它可以使用无参数或任意数量的参数进行调用。请检查这两个代码。(请记住这些是C代码,不是C++代码)
示例
#include<stdio.h>
void my_function() {
//some task
}
main(void) {
my_function(10, "Hello", "World");
}
输出
This program will be compiled successfully
Example
#include<stdio.h>
void my_function(void) {
//some task
}
main(void) {
my_function(10, "Hello", "World");
}
输出
[Error] too many arguments to function 'my_function'
在C++中,这两个程序都会失败。因此,我们可以理解在C中,int main()可
.........................................................