C优先顺序查询

时间:2014-08-22 07:36:06

标签: c operator-precedence

    #include <stdio.h>
    int main()
    {
        int x = 2, y = 0;
        int z = (y++) ? 2 : y == 1 && x;
        printf("%d\n", z);
        return 0;
    }

我不明白行z=(y++) ? y==1 &&x;

z=1如何来,请帮助我。

-ashutosh

7 个答案:

答案 0 :(得分:1)

?:是C条件运算符。

http://en.wikipedia.org/wiki/%3F:#C

参见你的C书,Kernighan的C语言编程语言&amp; Ritchie,第2版,2.11条件表达式。

宣言:

int z = (y++) ? 2 : y == 1 && x;

被解析为:

int z = ((y++) ? 2 : (y == 1)) && x;

答案 1 :(得分:1)

    int x = 2, y = 0;
    int z = (y++) ? 2 : y == 1 && x;

后增量首先运行(由于括号;即使没有它们支撑它也应该&#39; win&#39;因为它在表达式中的优先级也是最高的)并且它返回的值首先是操作数y,然后是增量,因此它变为

    int z = 0 ? 2 : 1 == 1 && 2;

现在涉及的运营商

  • =(作业)
  • ?:(三元条件)
  • ==(平等)
  • &&(逻辑和)

因此是他们之间的优先顺序:==&gt; &&&gt; ?:&gt; =。因此,表达式的评估阶段变为

int z = 0 ? 2 : true && 2;    // == evaluated to true 

int z = 0 ? 2 : true;         // 2 in boolean context evals to 2; true && true = true

int z = true;                 // ?: chooses the 2nd operand since the condition is false

int z = 1;                    // true implicitly converted to int gives 1

因此,您看到z被打印为1

答案 2 :(得分:1)

在这种任务中,我通常使用调试器。首先将它划分为三行,理解并逐行执行代码将更加简单

int z = (y++) ?
    2 : 
    y == 1 && x;

首先你检查一下。它是零。所以你去第3行。但是在比较之后y增加(所以y是1)。在第3行,你比较y和1(==的优先级高于&amp;&amp;),它返回某种真(可能是1)。在那之后你应用逻辑&amp;&amp;两个&#34;真&#34; (非零)值。所以它应该返回&#34; true&#34; (可能是1)

答案 3 :(得分:1)

此表达式 -

int z = (y++) ? 2 : y == 1 && x;

可以扩展为 -

if(y++){
   k = 2;
}
else{
  k = (y == 1 && x);
}

最初y0。当您执行y++时,它将取值0,并将y增加到1。 所以

if( 0 ){ // after evaluating y++, y becomes 1
   k = 2;
}
else{
  k = (y == 1 && x); // so here y==1 is true and x also non zero vale. so it returns 1
  // because the result of logical expression is 1 or 0
}

所以你将1作为输出!

答案 4 :(得分:0)

在第一个条件y++ y中因为后期增量将为零,因此它将进入第二个条件,
y == 1 && xx为2.且1&amp;&amp; 2 = 1,因此z为1。

int z = (y++) ? 2 : y == 1 && x;
          |         |        |
        y=0        y=1     1&&2 = 1

答案 5 :(得分:0)

1)(y ++)首先返回y的值,然后递增它,所以(y ++)等于零,但是y = 1

2)来自1的0)在C中被视为布尔值false,因此评估:运算符的右侧

3)1 == 1&amp;&amp; 2 =&gt; 1 == 1 =&gt; 1(1&amp;&amp; 2为真= 1)(1 == 1为真=&gt; 1)

4)结束为z = 1;

答案 6 :(得分:0)

The meaning of this operator is like this : 

(if this is true) ? do this : else do this;

In your case : 

y = 0;
int z = (y++) ? 2 : y == 1 && x; 

here,

if--
if y++ results in an integer value that is equivalent to true (i.e. if y++ >0 or y++ < 0
   but not 0, other than 0 any value is considered as true) , then the value 2 is 
   assigned to z, otherwise the else clause is executed.
   Here the if clause fails, since you have used post increment, which will increment  
   y after the 'if' condition is tested.( please google about post and pre increments
   for details )

else--
you have an expression( y == 1 && x ) whose result will ultimately be a boolean  
value, thus when the else clause will be executed it will assign the truth value of the 
of the expression to z.

truth value of your expression----

y==1  // this evaluates to true, since y++, increments y by 1 -----(1)
x     // since value of x is greater than 0, thus its truth value is 1.------- (2)

therefore,   (1) && (2) evaluates to true, since it is 'logical AND', which returns   
true if both its operands have their truth value as true.
And here y has value 1, and x has value 2, thus the truth value is 1.

Final solution--
Here the else clause is getting executed, which evaluates to 1, thus the value of   
y is 1.
For precedence of operators you can use man pages (man operator) if you are using  
linux, else google.