简体   繁体   English

将输入与两个字符进行比较,产生奇怪的警告

[英]comparing an input to two characters, creates odd warning

I am trying to compare an input to two different characters and execute the if condition if either of the conditions are met. 我试图将输入与两个不同的字符进行比较,如果满足两个条件之一,则执行if条件。

My code so far is: 到目前为止,我的代码是:

if(i == 'Y'||'y') {
        //code here
}

but the GCC compiler spits out the warning: 但是GCC编译器会发出警告:

warning: use of logical '||' with constant operand
      [-Wconstant-logical-operand]

why is this happening? 为什么会这样呢?

What you meant to say was: 您的意思是:

  if(i == 'Y'|| i == 'y')

The compiler is telling you exactly what you needed to know! 编译器会告诉您您需要知道的确切信息!

If you don't want to repeat the conditional (ie for a long list of options) you can use: 如果您不想重复该条件(即一长串选项),则可以使用:

if (strchr("Yy", i) != NULL) {
    ....
}

You need to #include <string.h> 您需要#include <string.h>

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

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