简体   繁体   English

C中的条件运算符一元

[英]Conditional operator unary in C

Can anyone explain to me or tell me how to fix this coding issue i have with conditional operator 谁能向我解释或告诉我如何解决条件运算符所存在的编码问题

it seems to always skip the conditional operator and just print out the second option even when i change server to be 0 or 1 它似乎总是跳过条件运算符,即使我将服务器更改为0或1,也只是打印出第二个选项

and it always prints out the second option SetB then SetA 并且总是打印出第二个选项SetB然后SetA

why does this not work? 为什么这不起作用? it is some kind of semantic error? 这是某种语义错误吗? or logical error? 还是逻辑错误?

#include <stdio.h>
#include <limits.h>

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

   int ch = -191;
   int x = UINT_MAX;
   int setA[5]={-1,-1,-1,-1,-1};
   int setB[5]={-2,-2,-2,-2,-2};
   int server = 1;
   int i=2;

   printf("%d %d\n",server==1?setA[i],setB[i]:setB[i],setA[i]);
   printf("%d %d\n",server==0?setA[i],setB[i]:setB[i],setA[i]);


  // printf("%u\n%u\n",ch,x);

}

The grammar for the ternary operator is: condition ? 三元运算符的语法为: 条件 expression : expression 表达表达

In your code the first expression is setA[i], setB[i] and the second expression is setB[i] . 在您的代码中,第一个表达式是setA[i], setB[i] ,第二个表达式是setB[i] Due to the way the language grammar works, the final comma is an argument separator (not a part of the second expression ), so your code is equivalent to: 由于语言语法的工作方式,最后一个逗号是参数分隔符(不是第二个表达式的一部分),因此您的代码等效于:

int a = (server == 1) ? setB[i] : setB[i];
printf("%d %d\n", a, setA[i]);

which of course is the same as: 当然,这与以下内容相同:

printf("%d %d\n", setB[i], setA[i]);

so you always get the same result regardless of the condition. 因此无论情况如何,您始终可以获得相同的结果。


If you are going to have many similar lines in a block of code, one possible solution is to set up some set aliases based on the current server: 如果您将在代码块中包含许多相似的行,则一种可能的解决方案是根据当前服务器设置一些集合别名:

// do this once:
int *set0, *set1;
if ( server == 1 )
    set0 = setA, set1 = setB;
else
    set0 = setB, set1 = setA;

// then use it like this    
printf("%d %d\n", set0[i], set1[i]);
server==1?setA[i],setB[i]:setB[i],setA[i]

Note the comma operator between the conditional operator, this expression doesn't result as you expected. 请注意条件运算符之间的逗号运算符,此表达式不会按您预期的那样生成。

Instead, I suggest using an if statement. 相反,我建议使用if语句。 It's more clear and less error-prone. 更清晰,更不易出错。

if (server == 1)
{
    printf("%d %d\n", setA[i], setB[i]);
}
else
{
    printf("%d %d\n", setB[i], setA[i]);
}

The expression 表达方式

server==1?setA[i],setB[i]:setB[i],setA[i]

is equivalent to: 等效于:

(server==1? (setA[i],setB[i]) : setB[i]), setA[i])

which is equivalent to 相当于

(server==1? setB[i] : setB[i]), setA[i]

which is equivalent to 相当于

setB[i], setA[i]

But, that's not what you wanted. 但是,那不是您想要的。 What you wanted was: 您想要的是:

if ( server == 1 )
{
  printf("%d %d\n", setA[i], setB[i]);
}
else if ( server == 0 )
{
  printf("%d %d\n", setB[i], setA[i]);
}

You should use the ternary operator like this: 您应该像这样使用三元运算符:

server == 1 ? printf("%d %d", setA[i], setB[i]):printf("%d %d", setB[i], setA[i]);

and similarly for second statement. 第二句话同样如此。

The way you use it: 使用方式:

if server == 1 如果server == 1
then setA[i], setB[i] evaluates to setB[i] , because that is property of comma , operator in C, and only the last statement is executed. 然后setA[i], setB[i]计算结果为setB[i]因为那是逗号的财产,运营商C,并且只执行的最后一条语句。

ie

If server == 1 如果server == 1
then 然后

printf("%d %d\\n",server==1?setA[i],setB[i]:setB[i],setA[i]);

will be evaluated as (due to high precedence of ?: operator over , operator) 将被评估为(由于?:运算符优先于,运算符的优先级较高)

printf("%d %d\\n",(server==1?(setA[i],setB[i]):setB[i]),setA[i]);

which is 这是

printf("%d %d\\n",setB[i], setA[i]);

and similarly 同样

printf("%d %d\\n",server==0?setA[i],setB[i]:setB[i],setA[i]);

will be evaluated simply as 将简单地评估为

printf("%d %d\\n",(server==0?(setA[i],setB[i]):setB[i]),setA[i])

which is again 再次

printf("%d %d\\n",setB[i],setA[i]);

You can parse similarly for server == 0 you will get same result. 您可以类似地为server == 0解析,您将获得相同的结果。

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

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