C语言 如何将整数作为命令行参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4796662/
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
how to take integers as command line arguments?
提问by Andrew
I've read a getopt() examplebut it doesn't show how to accept integers as argument options, like cvaluewould be in the code from the example:
我读过一个 getopt() 示例,但它没有显示如何接受整数作为参数选项,就像cvalue示例中的代码一样:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main (int argc, char **argv)
{
int aflag = 0;
int bflag = 0;
char *cvalue = NULL;
int index;
int c;
opterr = 0;
while ((c = getopt (argc, argv, "abc:")) != -1)
switch (c)
{
case 'a':
aflag = 1;
break;
case 'b':
bflag = 1;
break;
case 'c':
cvalue = optarg;
break;
case '?':
if (optopt == 'c')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\x%x'.\n",
optopt);
return 1;
default:
abort ();
}
printf ("aflag = %d, bflag = %d, cvalue = %s\n",
aflag, bflag, cvalue);
for (index = optind; index < argc; index++)
printf ("Non-option argument %s\n", argv[index]);
return 0;
}
If I ran the above as testop -c foo, cvaluewould be foo, but what if I wanted testop -c 42? Since cvalueis of type char *, could I just cast optargto be (int)? I've tried doing this without using getopt()and accessing argv[whatever]directly, and casting it as an integer, but I always end up with a large negative number when printing with %d. I'm assuming I'm not dereferencing argv[]correctly or something, not sure...
如果我按照上面的方式运行testop -c foo,cvalue将会是foo,但是如果我想要testop -c 42呢?既然cvalue是类型char *,我可以直接optarg转换为(int)吗?我已经尝试在不直接使用getopt()和访问的情况下执行此操作argv[whatever],并将其转换为整数,但是在使用%d. 我假设我没有argv[]正确取消引用或其他东西,不确定......
回答by Vlad H
You need to use atoi()to convert from string to integer.
您需要使用atoi()从字符串转换为整数。
回答by zwol
All of the answers above are broadly correct (Vikram.exe gets props for explaining whyyou have to call a library function, which nobody else bothered to do). However, nobody has named the correctlibrary function to call. Do not use atoi. Do not use sscanf.
上面的所有答案都大致正确(Vikram.exe 得到了解释为什么必须调用库函数的道具,而其他人都懒得这样做)。但是,没有人指定要调用的正确库函数。不要使用atoi. 不要使用sscanf.
Use strtol, or its relative strtoulif you don't want to allow negative numbers. Only these functions give you enough information when the input was nota number. For instance, if the user types
如果您不想允许负数strtol,请使用, 或其亲属strtoul。当输入不是数字时,只有这些函数才能为您提供足够的信息。例如,如果用户键入
./a.out 123cheesesandwich
atoiand sscanfwill cheerfully return 123, which is almost certainly not what you want. Only strtolwill tell you (via the endptr) that it processed only the first few characters of the string.
atoi并且sscanf会很高兴地返回 123,这几乎肯定不是您想要的。Onlystrtol会告诉您(通过endptr)它只处理字符串的前几个字符。
(There is no strtoi, but there is strtodif you need to read a floating-point number.)
(没有strtoi,但strtod如果您需要读取浮点数,则有。)
回答by Vikram.exe
As you have already used in your code, the prototype for mainfunction is
正如您在代码中已经使用的那样,main函数的原型是
int main (int argc, char** argv)
int main (int argc, char** argv)
What ever arguments are provided as command line arguments, they are passed to your 'program' as an array of char * (or simply strings). so if you invoke a prog as foo.exe 123, the first argument to foo.exewill be a string 123 and not an integer value of 123.
任何参数作为命令行参数提供,它们作为字符 *(或简单的字符串)数组传递给您的“程序”。因此,如果您调用 prog as foo.exe 123,则第一个参数foo.exe将是字符串 123 而不是整数值 123。
If you try casting the argument to integer (as you said) probably using some thing like: (int) argv[1], you will not get the integer value of first argument, but some memory address where the first arg is stored in your address space. To get an integer value, you must explicitly convert string value to integer value. For this, you can use atoifunction. Check THISman page for similar functions that can be used for conversion.
如果您尝试将参数转换为整数(如您所说),可能会使用以下内容:(int) argv[1],您将不会获得第一个参数的整数值,而是获得第一个参数存储在地址空间中的某个内存地址。要获得整数值,您必须显式地将字符串值转换为整数值。为此,您可以使用atoi函数。检查此手册页以获取可用于转换的类似功能。
回答by Sulla
atoi(), which means ascii to integer is the function to use. Similarly, atof()can be used to get float values.
atoi(),这意味着 ascii 到 integer 是要使用的函数。同样,atof()可用于获取浮点值。
回答by Mark Loeser
Use atoi.
使用atoi.
cvalue = atoi( optarg );
And declare cvalue as an int.
并将 cvalue 声明为int.
回答by stefan
In this situation i would go for a sscanf().
在这种情况下,我会选择sscanf().
回答by Pablo Santa Cruz
No, you can't just cast to convert it to an integer value. You need to transform it using sscanf, atoi, atol or similar function.
不,您不能只是将其转换为整数值。您需要使用 sscanf、atoi、atol 或类似函数对其进行转换。

