C 用户输入数组用作 if 语句
C user input to array used as if statement
我正在编写从命令行获取的代码。用户将在命令行中输入例如 ./a.out -l 2 4 6
。这里的目标是遍历数组并查看是否出现 '-l'
或 '-s'
。如果 '-l'
出现它使 x = 1 ,如果 '-s'
x = 2 如果 x = 0 都没有。现在抛出的问题是第 7 行和第 12 行指针和整数之间的比较。也是一个多-第 12 行的字符字符常量,我不确定为什么在第 9 行没问题时抛出它。我将如何更改我的 if
语句来解决抛出的问题?我的代码如下:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
int x;
for(; *argv != '[=10=]'; argv++){
if(*argv == '-l'){
x = 1;
}
else if(*argv == '-s'){
x = 2;
}
else{
x = 0;
}
}
printf("%d",x);
return 0;
}
字符串是用双引号指定的,而不是用于字符的单引号。此外,您不能使用 ==
来比较字符串。为此使用 strcmp
:
if(strcmp(*argv,"-l") == 0){
...
if(strcmp(*argv,"-s") == 0){
您的输出也不会像您预期的那样。每次检查下一个参数时都会覆盖 x
,因此结果将仅取决于最后一个参数。当满足两个条件之一时,您需要break
跳出循环。
除了其他人指出的错误之外,您可能在某些时候想要进行正确的命令行解析。在 POSIX 系统上,这是通过 unistd.h
header 中声明的 getopt()
库函数完成的(这是 而不是 "C standard" C 代码).
我已经通过一个选项 -x
扩展了您的要求,该选项采用一个整数参数来说明 x
应该设置为:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int x = 0; /* default zero for x */
int i, tmp;
int opt;
char *endptr;
/* the ':' in the string means "the previous letter takes an argument" */
while ((opt = getopt(argc, argv, "lsx:")) != -1) {
switch (opt) {
case 'l':
x = 1;
break;
case 's':
x = 2;
break;
case 'x':
/* validate input, must be integer */
tmp = (int)strtol(optarg, &endptr, 10);
if (*optarg == '[=10=]' || *endptr != '[=10=]')
printf("Illegal value for x: '%s'\n", optarg);
else
x = tmp;
break;
case '?': /* fallthrough */
default:
/* call routine that outputs usage info here */
puts("Expected -s, -l or -x N");
}
}
argc -= optind; /* adjust */
argv += optind; /* adjust */
printf("x = %d\n", x);
puts("Other things on the command line:");
for (i = 0; i < argc; ++i)
printf("\t%s\n", argv[i]);
return EXIT_SUCCESS;
}
运行它:
$ ./a.out -l
x = 1
Other things on the command line:
$ ./a.out -s
x = 2
Other things on the command line:
最后一个选项"wins":
$ ./a.out -s -x -78
x = -78
Other things on the command line:
在这里,-s
是最后一个:
$ ./a.out -s -x -78 -ls
x = 2
Other things on the command line:
$ ./a.out -s -q
./a.out: illegal option -- q
Expected -s, -l or -x N
x = 2
Other things on the command line:
解析在第一个 non-option:
处停止
$ ./a.out -s "hello world" 8 9 -x 90
x = 2
Other things on the command line:
hello world
8
9
-x
90
我正在编写从命令行获取的代码。用户将在命令行中输入例如 ./a.out -l 2 4 6
。这里的目标是遍历数组并查看是否出现 '-l'
或 '-s'
。如果 '-l'
出现它使 x = 1 ,如果 '-s'
x = 2 如果 x = 0 都没有。现在抛出的问题是第 7 行和第 12 行指针和整数之间的比较。也是一个多-第 12 行的字符字符常量,我不确定为什么在第 9 行没问题时抛出它。我将如何更改我的 if
语句来解决抛出的问题?我的代码如下:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
int x;
for(; *argv != '[=10=]'; argv++){
if(*argv == '-l'){
x = 1;
}
else if(*argv == '-s'){
x = 2;
}
else{
x = 0;
}
}
printf("%d",x);
return 0;
}
字符串是用双引号指定的,而不是用于字符的单引号。此外,您不能使用 ==
来比较字符串。为此使用 strcmp
:
if(strcmp(*argv,"-l") == 0){
...
if(strcmp(*argv,"-s") == 0){
您的输出也不会像您预期的那样。每次检查下一个参数时都会覆盖 x
,因此结果将仅取决于最后一个参数。当满足两个条件之一时,您需要break
跳出循环。
除了其他人指出的错误之外,您可能在某些时候想要进行正确的命令行解析。在 POSIX 系统上,这是通过 unistd.h
header 中声明的 getopt()
库函数完成的(这是 而不是 "C standard" C 代码).
我已经通过一个选项 -x
扩展了您的要求,该选项采用一个整数参数来说明 x
应该设置为:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int x = 0; /* default zero for x */
int i, tmp;
int opt;
char *endptr;
/* the ':' in the string means "the previous letter takes an argument" */
while ((opt = getopt(argc, argv, "lsx:")) != -1) {
switch (opt) {
case 'l':
x = 1;
break;
case 's':
x = 2;
break;
case 'x':
/* validate input, must be integer */
tmp = (int)strtol(optarg, &endptr, 10);
if (*optarg == '[=10=]' || *endptr != '[=10=]')
printf("Illegal value for x: '%s'\n", optarg);
else
x = tmp;
break;
case '?': /* fallthrough */
default:
/* call routine that outputs usage info here */
puts("Expected -s, -l or -x N");
}
}
argc -= optind; /* adjust */
argv += optind; /* adjust */
printf("x = %d\n", x);
puts("Other things on the command line:");
for (i = 0; i < argc; ++i)
printf("\t%s\n", argv[i]);
return EXIT_SUCCESS;
}
运行它:
$ ./a.out -l
x = 1
Other things on the command line:
$ ./a.out -s
x = 2
Other things on the command line:
最后一个选项"wins":
$ ./a.out -s -x -78
x = -78
Other things on the command line:
在这里,-s
是最后一个:
$ ./a.out -s -x -78 -ls
x = 2
Other things on the command line:
$ ./a.out -s -q
./a.out: illegal option -- q
Expected -s, -l or -x N
x = 2
Other things on the command line:
解析在第一个 non-option:
处停止$ ./a.out -s "hello world" 8 9 -x 90
x = 2
Other things on the command line:
hello world
8
9
-x
90