开发者

Error in ternary expression

Consider the following code which shows compile time error :

#include <stdio.h>

int main(int argc, char** argv)
{
   int x=5,y=0,z=2;
   int a=z?x,y?x:(y); // but z?x,y?x:y:z is not showing any error
    printf("%d",a);
return 0;
}

Pl开发者_JAVA百科ease help me explain the reason why z?x,y?x:y:z is not showing any error?


Why would it; it's valid and groups like this:

z?(x, (y?x:y)):z

The middle operand of the conditional expression can be any expression.


it is correct.. for each ? exactly one : will be there in ternary expressions that was absent in z?x,y?x:(y);


Comma , is not part of ternary expressions.


The z?x,y?x:y:z is two ternary expressions. I would write it as this:

z ? (x, y ? x : y) : z

There is always exactly one ? for each :.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜