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

下面贴一段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代码:

编译,链接后执行可得

What does int main(int argc, char *argv[]) mean?的更多相关文章

  1. main(int argc, char **argv)参数解读

    main(int argc, char **argv)参数解读 编译生成了test.exe ,然后在控制台下相应的目录下输入:test  1  2  3 4 argc就是一个输入了多少个参数,包括te ...

  2. int main( int argc, char **argv)

    1.参数 (有时参数是void) argc是程序运行时参数个数 argv是存储参数的数组,可以用char* argv[],也可以用char **argv. 例如编译一个hello.c的程序 1 #in ...

  3. int main(int argc,char* argv[])详解

    argc是命令行总的参数个数 argv[]是argc个参数,其中第0个参数是程序的全名,以后的参数命令行后面跟的用户输入的参数, 比如:       int   main(int   argc,   ...

  4. int main(int argc, char * argv[]) 里的异常处理

    #import <UIKit/UIKit.h> #import "AppDelegate.h" int main(int argc, char * argv[]) { ...

  5. main(int argc,char *argv[])

    #include<iostream.h> //ECHO.CPP void main(int argc,char *argv[]) { ;i<argc;i++)cout<< ...

  6. int main(int argc,char* argv[])参数详解

    argc是命令行总的参数个数 argv[]是argc个参数,其中第0个参数是程序的全名,以后的参数 命令行后面跟的用户输入的参数,比如: int main(int argc, char* argv[] ...

  7. int main(int argc,char* argv[]) 简单理解

    (1)第一个int代表整个main函数的返回值,若函数正常执行完毕,返回0,异常返回则是-1 (2)int argc代表命令行参数的总个数,既然是个数,那就是整型的,即:int; (3)char* a ...

  8. main()函数的输入参数 main(int argc, char** argv)

    一般简单的C++程序,main函数的写法都是 int main() {... ; return 0;},但是,如果在运行程序时需要有参数输入,可以是使用将主函数写成int main(int argv, ...

  9. main(int argc , char *argv[])

    #include <unistd.h>#include <stdlib.h>#include <stdio.h> int main(int argc, char * ...

  10. [c language] getopt 其参数optind 及其main(int argc, char **argv) 参数解释

    getopt被用来解析命令行选项参数.#include <unistd.h> extern char *optarg; //选项的参数指针extern int optind, //下一次调 ...

随机推荐

  1. 花1分钟配置远程DEBUG,开发效率翻倍,妹子直呼绝绝子

    当把一个工程部署到远程服务器后有可能出现意想不到错误,日志打印过多或者过少都影响问题排查的效率,这个时候可以通过远程调试的方式快速定位bug,提升工作效率.本文主要讲解如何使用Idea开发工具进行远程 ...

  2. springcloud04 Ribbbon

    Ribbbon虽然现在已经进入维护模式了,但是使用的还是很多的. 1.官网 https://github.com/Netflix/ribbon/wiki/Getting-Started 2.Ribbo ...

  3. HTTPS基础原理和配置-2

    〇.概述 作为概述,以下是本文要讲的内容.HTTPS 是什么? 每个人都可能从浏览器上认出 HTTPS,并对它有好感.然后再讲一遍基础知识,再详细讲一下协议版本,密码套件(Cipher Suites) ...

  4. Mybatis 区别-开发

    三.MyBatis 主流的ORM 支持java .NET Ruby三种语言,MyBatis是对JDBC的封装 ORM框架Hibernate 区别: 1)sql 优化方面 Hibernate 使用 HQ ...

  5. 花10几元买ESP32-C3,体验一下MicroPython (和CircuitPython)

    ESP32是近年很火的国产低成本MCU系列. 买了芯片ESP32-C3的模组安信可 ESP-C3-32S的开发板安信可 NodeMCU ESP-C3-32S-Kit .开发板很小,没有任何多余的东西, ...

  6. Git Please commit your changes or stash them before you merge.

    本地分支和远程修改了同一个文件代码,pull远程分支的代码的时候会文件冲突 出现这个错误 Please commit your changes or stash them before you mer ...

  7. Linux——CentOS7无法ping通外网问题

    出现问题: 当在终端中出现 ping: unknown host www.baidu.com 或 ping: www.baidu.com: 未知的名称或服务 时,该如何解决? 解决方法: 1.使用ro ...

  8. MySQL5.7升级版本到8.0

    升级二进制包安装的MySQL In-Place Upgrade(替代升级) 替代升级涉及到shutdown down旧版本的MySQL,用新版本的包替代旧版本的二进制包,用存在的数据文件目录重启MyS ...

  9. Linux开发C++

    首先在windows上安装linux系统. VM简介 VMware Workstation中文版是一个"虚拟 PC"软件.它使你可以在一台机器上同时运行二个或更多 Windows. ...

  10. @Resource 与@Autowired 区别

    @Resource与@Autowired两个的作用是相同的,很多人因为个人习惯选择用的不同,但是他们两个本质上还有有一定的区别的,可以参考区别来选择具体用哪一个: @Autowired与@Resour ...