What does int main(int argc, char *argv[]) mean?
忽然发现自己不理解许多代码中这行的含义是什么。。。(汗颜)
下面贴一段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?的更多相关文章
- main(int argc, char **argv)参数解读
main(int argc, char **argv)参数解读 编译生成了test.exe ,然后在控制台下相应的目录下输入:test 1 2 3 4 argc就是一个输入了多少个参数,包括te ...
- int main( int argc, char **argv)
1.参数 (有时参数是void) argc是程序运行时参数个数 argv是存储参数的数组,可以用char* argv[],也可以用char **argv. 例如编译一个hello.c的程序 1 #in ...
- int main(int argc,char* argv[])详解
argc是命令行总的参数个数 argv[]是argc个参数,其中第0个参数是程序的全名,以后的参数命令行后面跟的用户输入的参数, 比如: int main(int argc, ...
- int main(int argc, char * argv[]) 里的异常处理
#import <UIKit/UIKit.h> #import "AppDelegate.h" int main(int argc, char * argv[]) { ...
- main(int argc,char *argv[])
#include<iostream.h> //ECHO.CPP void main(int argc,char *argv[]) { ;i<argc;i++)cout<< ...
- int main(int argc,char* argv[])参数详解
argc是命令行总的参数个数 argv[]是argc个参数,其中第0个参数是程序的全名,以后的参数 命令行后面跟的用户输入的参数,比如: int main(int argc, char* argv[] ...
- int main(int argc,char* argv[]) 简单理解
(1)第一个int代表整个main函数的返回值,若函数正常执行完毕,返回0,异常返回则是-1 (2)int argc代表命令行参数的总个数,既然是个数,那就是整型的,即:int; (3)char* a ...
- main()函数的输入参数 main(int argc, char** argv)
一般简单的C++程序,main函数的写法都是 int main() {... ; return 0;},但是,如果在运行程序时需要有参数输入,可以是使用将主函数写成int main(int argv, ...
- main(int argc , char *argv[])
#include <unistd.h>#include <stdlib.h>#include <stdio.h> int main(int argc, char * ...
- [c language] getopt 其参数optind 及其main(int argc, char **argv) 参数解释
getopt被用来解析命令行选项参数.#include <unistd.h> extern char *optarg; //选项的参数指针extern int optind, //下一次调 ...
随机推荐
- JSP第九次作业
1.建库,建表2个用户表(id,用户名,密码)邮件表(id,发件人,收件人,标题,内容,发送时间,状态) 2.建model层entity,dao包 3.登陆,注册,登陆后显示全部邮件 dao 1 pa ...
- element ui el-date-picker 禁止选择指定日期
1.日期选择器组件代码 <el-col :span="20"> <el-form-item label="活动起始日期值" prop=&quo ...
- 学习Java Day22
今天学习了如何在包中增加类,想要将包放入类中,就必须将包的名字放在源文件的开头,即放在定义这个包中各个类的代码之前
- Slave_IO_Running: Connecting--一种问题的解决方案
主要有三个原因: 1.网络不同 2.密码不对 3.pos不对 这里只介绍我碰到的问题--不能远程连接数据库.即在从机上对主机进行以下命令 mysql -u**** -p**** -h192.168.* ...
- Cesium Ellipsoid(十四)
由方程(x/A)^2+(y/b)^2+(z/c)^2=1在笛卡尔坐标系中定义的二次曲面.Cesium主要用来表示行星体的形状.通常使用提供的常量之一,而不是直接构造此对象. 不用new,直接就可以使用 ...
- JZOJ 4216.平方和
\(\text{Problem}\) 维护一个序列 支持插入一个数,区间加,询问区间平方和 \(\text{Solution}\) 平衡树很模板的题了 考场打 \(fhq-treap\) 毫无悬念过了 ...
- 【CTO变形记】整体系统思维-从现象到本质
前言:我们的⼤脑⾥的认知不是⼀块⽩板,⽽是写满着密密麻麻对这个世界形成的各种观念.信念.塞满了对事物的各个表象,我们脑中的表象世界,对应着外部世界的各种事物. 如果感觉本篇看起来有点不适应,可以看看之 ...
- ASP.NET Core - 依赖注入(三)
4. 容器中的服务创建与释放 我们使用了 IoC 容器之后,服务实例的创建和销毁的工作就交给了容器去处理,前面也讲到了服务的生命周期,那三种生命周期中对象的创建和销毁分别在什么时候呢.以下面的例子演示 ...
- PostgreSQL事务隔离级别
一.概念 并发控制是多个事务在并发运行时,数据库保证事务一致性(Consistency)和隔离性(Isolation)的一种机制.PostgreSQL使用了多版本并发控制技术的一种变体:快照隔离San ...
- Spring Boot如何自定义监控指标
1.创建项目 pom.xml引入相关依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=" ...