C 语言的 argc、argv 干嘛的?
source link: http://blog.tangzhixiong.com/post-0008-argc-argv-in-c.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
C 语言的 argc、argv 干嘛的?
C 语言的 argc、argv 干嘛的?
以一个例子,说明 c 语言中传入 main 函数的 argc,argv 是什么东西。
源文件
argcargv.c
#include <stdio.h>
int main( int argc, char **argv )
{
int a = atoi( argv[1] );
int b = atoi( argv[2] );
printf( "a + b = %d + %d = %d\n", a, b, a+b );
return 0;
}
编译后调用
对于 Linux 平台:
# 编译
$ gcc argcargv.c -o argcargv
# 运行
$ ./argcargv 5 7
a + b = 5 + 7 = 12
对于 Windows:
先到 C:/Windows/System32
文件夹下创建一个文件 mycl.bat
: 1
@echo off
%comspec% /c ""c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" amd64 && cl.exe %*"
再到源码文件夹下,按住 Shift – 鼠标右键 – 在此处打开命令窗口(W),在 cmd 中输入:
# 编译
$ mycl argcargv.c
# 运行
$ ./argcargv 5 7
a + b = 5 + 7 = 12
总结
就是在程序运行的时候传递参数。因为 c 语言中没有“StringList”类型,所以只能用 **char
2 把命令行参数传递给程序。
这里放一段 Boost bcp 解析命令行的源码供大家体会体会。
for(int i = 1; i < argc; ++i)
{
if(0 == std::strcmp("-h", argv[i])
|| 0 == std::strcmp("--help", argv[i]))
{
show_usage();
return 0;
}
if(0 == std::strcmp("-v", argv[i])
|| 0 == std::strcmp("--version", argv[i]))
{
...
return 0;
}
...
else if(0 == std::strcmp("--svn", argv[i]))
{
papp->enable_svn_mode();
}
...
else if(0 == std::strncmp("--boost=", argv[i], 8))
{
papp->set_boost_path(argv[i] + 8);
}
else if(0 == std::strncmp("--namespace=", argv[i], 12))
{
papp->set_namespace(argv[i] + 12);
}
...
else if(argv[i][0] == '-')
{
std::cout << "Error: Unknown argument " << argv[i] << std::endl;
show_usage();
return 1;
}
else
{
positional_args.push_back(argv[i]);
}
}
附
TODO: 一些 C/C++ 命令行解析的库。
Related Issues not found
Please contact @district10 to initialize the comment
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK