忽然发现自己不理解许多代码中这行的含义是什么。。。(汗颜)

下面贴一段stackoverflow上面的回答:

argv and argc are how command line arguments are passed to main() in C and C++.

argc will be the number of strings pointed to by argv. This will (in practice) be 1 plus the number of arguments, as virtually all implementations will prepend the name of the program to the array.

The variables are named argc (argument count) and argv (argument vector) by convention, but they can be given any valid identifier: int main(int num_args, char** arg_strings) is equally valid.

They can also be omitted entirely, yielding int main(), if you do not intend to process command line arguments.

Try the following program:

#include <iostream>

int main(int argc, char** argv) {
std::cout << "Have " << argc << " arguments:" << std::endl;
for (int i = 0; i < argc; ++i) {
std::cout << argv[i] << std::endl;
}
}

Running it with ./test a1 b2 c3 will output:

Have 4 arguments:
./test
a1
b2
c

int main(int argc, char *argv[])是Linux和Unix中的标准写法,在命令行中传参数这样可以接收参数,argc表示后面的参数个数+1(这里的加1代表程序运行的全路径名或程序的名字,如上面代码输出的./test),argv为参数的字符串数组的指针。int main()则当不传入参数时使用。

但是还有一种写法:int main(int argc, char* argv[], char* envp[]),后面的char* envp[]是用来取得系统的环境变量

如图为test.cpp代码:

编译,链接后执行可得

最新文章

  1. 几个常用Json组件的性能测试
  2. 在ubuntu上面配置nginx实现反向代理和负载均衡
  3. Android Studio的下载和安装教程(从ADT到AS)
  4. 大数据系列(3)——Hadoop集群完全分布式坏境搭建
  5. Android 屏幕滑动事件
  6. iOS中MVC设计模式
  7. sematext
  8. 搭建Nginx图片服务器
  9. redis持久化(摘录)
  10. 使用node.js编写脚本将JSON数据转换为SQL语句
  11. 敏捷开发(2)-Scrum
  12. C:指针函数一些误区
  13. asp.net core 教程(五)-配置
  14. BEX5下集成FullCalendar
  15. new关键字创建对象带不带{}的区别
  16. MVVM框架在unity开发中的使用
  17. DOS批处理前言
  18. Swagger中显示注释
  19. TP5报错variable type error: array
  20. 【转】MFC 无边框窗口的拖动

热门文章

  1. vue的异步组件
  2. ArcGIS Pro SDK 002 对数据文件的读取和操作
  3. 软件教程 | Jupyter&amp;stata之stata_kernel攻略
  4. 【NOI2014】随机数生成器
  5. JZOJ 2936. 【NOIP2012模拟8.9】逐个击破
  6. ctf命令执行刷题
  7. xampp修改mysql数据库密码(测试成功)
  8. Vulhub 漏洞学习之:Fastjson
  9. 两个集合之间数据过滤linq
  10. Prometheus安装部署(主体)