tags:

views:

80

answers:

4

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;
}

Please help me explain the reason why z?x,y?x:y:z is not showing any error?

A: 

Comma , is not part of ternary expressions.

too much php
then why `z?x,y?x:y:z` is not showing any eror
Bipul
+2  A: 

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.

Charles Bailey
A: 

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 :.

Dietrich Epp
I wouldn't write it at all :)
erikkallen
+1  A: 

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

mukesh