C/C++ 中的“int main()”和“int main(void)”的区别
C
在 C 编程语言中,如果函数签名没有任何参数,那么它可以将多个参数作为输入,但 C++ 中并非如此。如果在 C++ 中将参数传递给此类函数,则编译将失败。这是 C 中的 int main() 和 int main(void) 相同的原因,但 int main(void) 是一种更好的方法,它限制了用户将多个参数传递给 main 函数。
示例 (C)
#include <stdio.h>
int main() {
static int counter = 3;
if (--counter){
printf("%d ", counter);
main(5);
}
}输出
2 1
示例 (C++)
#include <iostream>
using namespace std;
int main() {
static int counter = 3;
if (--counter){
cout << counter;
main(5);
}
}输出
main.cpp: In function 'int main()': main.cpp:10:13: error: too many arguments to function 'int main()' main(5); ^ main.cpp:5:5: note: declared here int main() ^~~~
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP