一、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) ;
}
}
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 的说明:
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!