Linux中相关介绍:main函数参数(argc、argv)+相关函数介绍

一、main函数参数

1、参数介绍

1.1、介绍

int main(int argc, char *argv[])
	参数功能:用于传递命令行参数的参数
	参 数:
		@argc:一共有几个参数(包括./test)
		@argv:指向字符串数组的指针,每个字符串表示一个命令行参数
			argv[0]:./test
			argv[n]:依次向后的参数

1.2、代码示例

#include <stdio.h>
 
int main(int argc, char *argv[]) {
    printf("%d\n", argc);
    
    for (int i = 0; i < argc; i++) {
        printf(" %d: %s\n", i, argv[i]);
    }
    
    return 0;
}
运行结果:
aaa@ubuntu:/mnt/hgfs/LinuxShare$ ./a.out aa bb cc
4
 0: ./a.out
 1: aa
 2: bb
 3: cc
aaa@ubuntu:/mnt/hgfs/LinuxShare$ 

2、atoi函数/itoa函数

2.1、函数介绍

#include <stdlib.h>
函数原型:
	int atoi(const char* str);
	功 能:将字符串转为整数
	参 数:
		str:要转换的字符串。
	返回值:
		如果成功,则返回相应的整数值;
		如果发生错误,则返回0

2.2、函数使用:使用argv传参后,转换为数字,可以得到自己想要的数字

3、getopt/getopt_long

3.1、getopt介绍

#include <unistd.h> 
函数原型:
	int getopt(int argc, char * const argv[], const char *optstring);
	功 能:分析命令行参数
	参 数:
		@argc:传主函数的argc整数
		@argc:传主函数的argv数组指针
		@optstring:
			1.单个字符,表示选项。
			2.单个字符后接一个冒号:表示该选项后必须跟一个参数。
			  参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。
			3 单个字符后跟两个冒号,表示该选项后可以跟一个参数,也可以不跟。
			  如果跟一个参数,参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。
			 

3.2、getopt函数示例

#include <stdio.h>
#include<unistd.h>
 
int main(int argc, char *argv[])
{
    int ch;
    opterr = 0;
   
    while((ch = getopt(argc,argv,"a:bcde::"))!= -1)
    {
        switch(ch)
        {
            case 'a': printf("option a:%s\n",optarg); break;
            case 'b': printf("option b:\n"); break;
            default: printf("other option :%c\n",ch);
        }
        //printf("optopt %c\n",optopt);
    }
    return 0;
}
运行结果
aaa@ubuntu:/mnt/hgfs/LinuxShare$ ./a.out -a123
option a:123
aaa@ubuntu:/mnt/hgfs/LinuxShare$ ./a.out -a 123
option a:123
aaa@ubuntu:/mnt/hgfs/LinuxShare$ ./a.out -b
option b:
aaa@ubuntu:/mnt/hgfs/LinuxShare$ ./a.out -d
other option :d
aaa@ubuntu:/mnt/hgfs/LinuxShare$ ./a.out -a123 -bc
option a:123
option b:
other option :c

3.3、getopt_long函数介绍

#include <getopt.h>  
函数原型:
	int getopt_long(int argc, char * const argv[],  const char *optstring,  
		           	const struct option *longopts, int *longindex);
	参 数:
		@argc:传主函数的argc整数
		@argc:传主函数的argv数组指针
		@optstring:
			1.单个字符,表示选项。
			2.单个字符后接一个冒号:表示该选项后必须跟一个参数。
			  参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg。
			3 单个字符后跟两个冒号,表示该选项后可以跟一个参数,也可以不跟。
			  如果跟一个参数,参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。
		@longopts:指向struct option数组的第一个元素的指针
			struct option的说明:
				/*
				name: 长选项名
				has_arg: 是否带参数或可选参数,这个值在getopt.h中有宏定义,如下:
				     # define no_argument        0
				     # define required_argument  1
				     # define optional_argument  2
				flag: 确定函数返回值的情况,如果flag==NULL,则识别选项后返回val
					(常用的如:设置val为长命令的短命令字符);否则,识别后getopt_long返回0,
				 	 flag指向一个设置到val的变量;
				val: 设置为返回值,或者是flag指向的变量;
					这里要注意不要写-1到val,否则其作用是getopt_long返回-1,然后停止解析选项;
				 
				[注意] longopts的最后一个元素必须是全0填充,否则会报段错误
				*/
				 
				struct option {
				    const char *name; 
				    int has_arg;  
				    int *flag;
				    int val;
				};
		@longindex:如果非NULL,则是返回识别到struct option数组中元素的位置指针

3.4、getopt_long函数示例

#include <stdio.h>
#include <getopt.h>
char *l_opt_arg;
char* const short_options = "nbl:";
struct option long_options[] = {
	{ "name", 0, NULL, 'n' },
	{ "bf_name", 0, NULL, 'b' },
	{ "love", 1, NULL, 'l' },
	{ 0, 0, 0, 0},
};

int main(int argc, char *argv[])
{
	int c;
	while((c = getopt_long (argc, argv, short_options, long_options, NULL)) != -1)
	{
		switch (c)
		{
			case 'n':
					printf("My name is XL.\n");
					break;
			case 'b':
					printf("His name is ST.\n");
					break;
			case 'l':
					l_opt_arg = optarg;
					printf("Our love is %s!\n", l_opt_arg);
					break;
		}
	}
	return 0;
}
运行结果:
aaa@ubuntu:/mnt/hgfs/LinuxShare$ ./a.out -bnl forever
His name is ST.
My name is XL.
Our love is forever!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好好睡觉好好吃饭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值