其中main前的int为返回类型自不必说。
main是主函数,程序开始执行时所要进入的入口函数。
int argc中 argc为执行本程序时所输入的参数的个数。
char * argv[]是执行本程序时所输入的参数的数组。
如我在linux下执行test.cpp函数。
#include <iostream>
using namespace std;
int main(int argc, char * argv[])
{
for (int i = 0; i < argc; i++)
{
cout<<argv[i]<<endl;
}
return 0;
}
第一步:先编译test.cpp 命令为“g++ test.cpp -o test”
第二部:执行test 命令为“ ./test” (如果这里什么都不输入)
执行后结果如下图
如果执行命令为 “./test 111 222 333 444 555”即在命令后加上5个参数
执行后结果如下图
即argc是你输入的参数的个数 5,argv[0] = “111”, argv[1] = “222”, ······。