C++(2)-----------------------argc&argv

argc与argv是main函数的参数,由ANSI-C规定

ANSI-C(美国国家标准协会,C的第一个标准ANSI发布)在C89/C99中main()函数主要形式为:
(1).int main(void)
(2).int main(int argc,char *argv[]) = int main(int argc,**char argv).
其参数argc和argv用于运行时,把命令行参数传入主程序.其中ARG是指arguments,即参数。

https://publications.gbdirect.co.uk//c_book/chapter10/arguments_to_main.html

1、作用

1.1main的形参的作用

main的形参一般有两个参数:argc和argv。

第一个argc(arguement count),其值=输入参数的个数,就是计数作用。
第二个argv(arguement vector),是指向字符串的指针数组,一般类型为是“char指针数组”。

第一个参数argv[0]一定是程序的名称,并且包含了程序所在的完整路径,所以确切的说需要我们输入的main函数的参数个数应该是argc-1个。

这些参数由主机系统的命令行 system’s command line interpreter或作业控制语言( job control language,比如ros中的.launch文件)传递给程序。

对于那些编写在宿主环境( DOS or UNIX)中运行的程序,main的参数可以为程序提供参数。通常,这个工具用于指导程序执行其任务的方式。比如,通过程序的参数向程序提供文件名是非常常见的

1.2main的返回值的作用:

由于C程序必须有main()函数为入口,而且它不能被其他函数调用(可以调用自身),因此不能再程序内部取得实际值.那么在何处把实参赋值给main函数的形参呢?这就需要调用"运行"或"DOS提示符",在调用可执行程序exe时,编译器会帮助我们将输入参数的信息放入main函数的参数列表中传参.

main是一个返回整数的函数。在DOS或UNIX等托管环境中,此值或退出状态被传递回命令行解释器。例如,在UNIX下,exit状态用于指示程序成功完成(零值)或发生错误(非零值)。标准采用了此约定;exit(0)用于将“success”返回到其宿主环境,任何其他值用于指示失败。如果宿主环境本身使用不同的编号约定,exit将执行必要的转换。由于转换是由实现定义的,因此现在认为使用<stdlib.h>中定义的值:EXIT_SUCCESS和EXIT_FAILURE是更好的做法。

2、argc&argv的使用

当数组被传递给函数时,数组的名称将转换为其第一个元素的地址。这意味着我们也可以将argv声明为char**argv;这两个声明在这个上下文中是等价的。

当程序启动时,main的参数将被初始化以满足以下条件:
(1)argc大于零。
(2)argv[argc]是空指针。
(3)argv[0]到argv[argc-1]是指向字符串的指针,其含义将由程序确定。

argv[0]将是一个包含程序名称的字符串如果不可用,则为空字符串argv的其余元素表示提供给程序的参数。在只支持单大写字符的情况下,这些字符串的内容将以小写形式提供给程序。

3、举例说明

To illustrate these points, here is a simple program which writes the arguments supplied to main on the program’s standard output.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
while(argc–)
printf("%s\n", *argv++);
exit(EXIT_SUCCESS);
}

Example 10.1

If the program name is show_args and it has arguments abcde, text, and hello when it is run, the state of the arguments and the value of argv can be illustrated like this:
Diagram showing the relationship between ‘argc’ and ‘argv’ and the strings that elements of ‘argv’ point to
Figure 10.1. Arguments to a program

Each time that argv is incremented, it is stepped one item further along the array of arguments. Thus after the first iteration of the loop, argv will point to the pointer which in turn points to the abcde argument. This is shown in Figure 10.2.
Diagram showing the changes to the arrangement in Figure 10.1 after incrementing ‘argv’ so that it points to the next element in the array of pointers
Figure 10.2. Arguments to a program after incrementing argv

On the system where this program was tested, a program is run by typing its name and then the arguments, separated by spaces. This is what happened (the $ is a prompt):

$ show_args abcde text hello
show_args
abcde
text
hello
$

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值