开发者

Error in ternary expression

开发者 https://www.devze.com 2022-12-23 02:23 出处:网络
Consider the following code which shows compile time error : #include <stdio.h> int main(int argc, char** argv)

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

精彩评论

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

关注公众号