简体   繁体   English

C-用于循环中argv的scanf char

[英]C - scanf char used for argv in loop

actually struggling with my code in C. I've added some program arguments and I want to print those beggining with letter user chooses. 实际上在C语言中苦苦挣扎。我已经添加了一些程序参数,并且我想打印那些以字母用户选择开头的代码。 Glad for any advice. 很高兴提供任何建议。

Thank you 谢谢

**Build messages:**
warning: multi-character character constant
warning: left-hand operand of comma expression has no effect 

int main(int argc, char *argv[]) {
    int i;
    char firstLetter;

    printf("What city r u looking for?\n");
    scanf("%c", &firstLetter);

    for(i=1;i<argc;i++) {
        if (argv[i][0] == '%c', "firstLetter") { //fault here?
            printf("%s\n", argv[i]);
        }
    }
    return 0;
}

This one works great: 这个效果很好:

int main(int argc, char *argv[]) {
    int i;
    for(i=1;i<argc;i++) {
        if (argv[i][1] == 'r') {
            printf("%s\n", argv[i]);
        }
    }
    return 0;
}

The error in your code is an issue with placing both an invalid character cosntant and a constant string literal inside of your if statement: 代码中的错误是在if语句中同时放置无效字符余弦和常量字符串文字的问题:

if (argv[i][0] == '%c', "firstLetter") //fault here?
{
    printf("%s\n", argv[i]);
}

This if statement's condition has several issues. 这个if语句的条件有几个问题。 First, '%c' is an invalid statement in general because anything surrounded by single quotes is expected to be a one-character literal. 首先, '%c'通常是无效的语句,因为任何用单引号引起来的东西都应该是一个字符的文字。 Therefore, '%c' would be invalid because it is two characters. 因此, '%c'将是无效的,因为它是两个字符。

Next, you are comparing '%c' , but then you are discarding the result. 接下来,您正在比较'%c' ,但是随后您将丢弃结果。 The comma operator in C is rather useful and surprisingly underused. C中的逗号运算符非常有用,而且使用不足。 Within conditional expressions, the comma operator causes everything to the left of it to be ignored, and indicates that only the item(s) to the right of the comma should be considered when evaluating the condition. 在条件表达式中,逗号运算符会导致忽略其左侧的所有内容,并表示在评估条件时仅应考虑逗号右侧的项目。 As an example if you say, if ( i = i+2, x != 'y' ){ ... } , then this will actually cause i to be incremented by two, then the comma causes the value to be ignored before the x != 'y' comparison is evaluated. 例如,如果您说( if ( i = i+2, x != 'y' ){ ... } ,则实际上会使i增加2,然后逗号导致该值在被忽略之前评估x != 'y'比较。 So, in your case, it might actually be valid to say if (argv[i][0] == 'c', "firstLetter") . 因此,在您的情况下,说if (argv[i][0] == 'c', "firstLetter")实际上可能是有效的。 (Although, the result would still not be what you want.) In this case, argv[i][0] == 'c' would be evaluated, and then the result of that evaluation would discarded. (尽管结果仍然不是您想要的。)在这种情况下,将对argv[i][0] == 'c'进行评估,然后将该评估的结果丢弃。 After that is discarded, "firstLetter" would be evaluated for its truthiness. 丢弃之后,将评估"firstLetter"的真实性。 "firstLetter" is a truthy value, the condition if (argv[i][0] == 'c', "firstLetter") would almost certainly evaluate to true. "firstLetter"是真实值, if (argv[i][0] == 'c', "firstLetter")条件if (argv[i][0] == 'c', "firstLetter")几乎可以肯定为真。 (Assuming your compiler lets you compile that.) (假设您的编译器允许您对其进行编译。)

Focusing on your specific use case, I would venture to guess you wanted to say if (argv[i][0] == firstLetter) , which will compare the first letter of the argv[i] string against the character stored in the firstLetter variable. 着眼于您的特定用例,我if (argv[i][0] == firstLetter)冒险猜测您是否想说if (argv[i][0] == firstLetter) ,它将把argv[i]字符串的第一个字母与存储在firstLetter中的字符进行firstLetter变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM