C语言 C - 读取命令行参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5157337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C - reading command line parameters
提问by Waypoint
I have made little program for computing pi (π) as an integral. Now I am facing a question how to extend it to compute an integral, which will be given as an extra parameter when starting an application. How do I deal with such a parameter in a program?
我做了一个小程序来计算 pi (π) 作为一个积分。现在我面临一个问题,如何扩展它来计算积分,该积分将在启动应用程序时作为额外参数给出。如何在程序中处理这样的参数?
回答by wkl
When you write your main function, you typically see one of two definitions:
当您编写 main 函数时,您通常会看到以下两个定义之一:
int main(void)int main(int argc, char **argv)
int main(void)int main(int argc, char **argv)
The second form will allow you to access the command line arguments passed to the program, and the number of arguments specified (arguments are separated by spaces).
第二种形式将允许您访问传递给程序的命令行参数,以及指定的参数数量(参数用空格分隔)。
The arguments to mainare:
的论据main是:
int argc- the number of arguments passed into your program when it was run. It is at least1.char **argv- this is a pointer-to-char *. It can alternatively be this:char *argv[], which means 'array ofchar *'. This is an array of C-style-string pointers.
int argc- 程序运行时传递给程序的参数数量。至少是1。char **argv- 这是一个指向- 的指针char *。它也可以是这样的:char *argv[],这意味着“数组char *”。这是一个 C 风格的字符串指针数组。
Basic Example
基本示例
For example, you could do this to print out the arguments passed to your C program:
例如,您可以这样做以打印出传递给 C 程序的参数:
#include <stdio.h>
int main(int argc, char **argv)
{
for (int i = 0; i < argc; ++i)
{
printf("argv[%d]: %s\n", i, argv[i]);
}
}
I'm using GCC 4.5 to compile a file I called args.c. It'll compile and build a default a.outexecutable.
我正在使用 GCC 4.5 编译我调用的文件args.c。它将编译并构建一个默认的a.out可执行文件。
[birryree@lilun c_code]$ gcc -std=c99 args.c
Now run it...
现在运行它...
[birryree@lilun c_code]$ ./a.out hello there
argv[0]: ./a.out
argv[1]: hello
argv[2]: there
So you can see that in argv, argv[0]is the name of the program you ran (this is not standards-defined behavior, but is common. Your arguments start at argv[1]and beyond.
所以,你可以看到,在argv,argv[0]是你运行该程序的名称(这不是标准定义的行为,但很常见。你的论点开始于argv[1]和超越。
So basically, if you wanted a single parameter, you could say...
所以基本上,如果你想要一个参数,你可以说......
./myprogram integral
./myprogram integral
A Simple Case for You
一个简单的案例
And you could check if argv[1]was integral, maybe like strcmp("integral", argv[1]) == 0.
你可以检查是否argv[1]是integral,也许像strcmp("integral", argv[1]) == 0。
So in your code...
所以在你的代码...
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
if (argc < 2) // no arguments were passed
{
// do something
}
if (strcmp("integral", argv[1]) == 0)
{
runIntegral(...); //or something
}
else
{
// do something else.
}
}
Better command line parsing
更好的命令行解析
Of course, this was all very rudimentary, and as your program gets more complex, you'll likely want more advanced command line handling. For that, you could use a library like GNU getopt.
当然,这都是非常初级的,随着您的程序变得越来越复杂,您可能需要更高级的命令行处理。为此,您可以使用GNU 之getopt类的库。
回答by pmg
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int i, parameter = 0;
if (argc >= 2) {
/* there is 1 parameter (or more) in the command line used */
/* argv[0] may point to the program name */
/* argv[1] points to the 1st parameter */
/* argv[argc] is NULL */
parameter = atoi(argv[1]); /* better to use strtol */
if (parameter > 0) {
for (i = 0; i < parameter; i++) printf("%d ", i);
} else {
fprintf(stderr, "Please use a positive integer.\n");
}
}
return 0;
}
回答by Validus Oculus
Parsing command line arguments in a primitive way as explained in the above answers is reasonable as long as the number of parameters that you need to deal with is not too much.
只要您需要处理的参数数量不是太多,以上述答案中解释的原始方式解析命令行参数是合理的。
I strongly suggest you to use an industrial strength library for handling the command line arguments.
我强烈建议您使用工业强度库来处理命令行参数。
This will make your code more professional.
这将使您的代码更加专业。
Such a library for C++ is available in the following website. I have used this library in many of my projects, hence I can confidently say that this one of the easiest yet useful library for command line argument parsing. Besides, since it is just a template library, it is easier to import into your project. http://tclap.sourceforge.net/
可以在以下网站中找到这样的 C++ 库。我在我的许多项目中都使用过这个库,因此我可以自信地说,这是用于命令行参数解析的最简单但有用的库之一。此外,由于它只是一个模板库,因此更容易导入到您的项目中。 http://tclap.sourceforge.net/
A similar library is available for C as well. http://argtable.sourceforge.net/
类似的库也可用于 C。 http://argtable.sourceforge.net/
回答by AndreDurao
There's also a C standard built-in library to get command line arguments: getopt
还有一个 C 标准内置库来获取命令行参数:getopt
You can check it on Wikipediaor in Argument-parsing helpers for C/Unix.

