“ int main()”和“ int main(void)”之间的C / C ++区别
C
在C编程语言中,如果函数签名没有任何参数,则它可以采用多个参数作为输入,但对于C++而言,情况并非如此。如果将参数传递给C++中的此类函数,则编译将失败。这是因为intmain()和intmain(void)在C中相同,但是intmain(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()^~~~
热门推荐
10 医院探望朋友祝福语简短
11 毕业祝福语简短女朋友
12 春节虎年爸妈祝福语简短
13 儿童毕业祝福语 简短6
14 开工仪式横幅祝福语简短
15 孩子日常祝福语大全简短
16 清晨寄语诗句祝福语简短
17 结婚送亲认亲祝福语简短
18 虎年喝酒拜年祝福语简短