-1

This prints 4. Why?

I'm aware how ternary operators work but this makes it complicated.

printf("%d", 0 ? 1 ? 2 : 3 : 4 );

Also this prints d. ???

int x=0, y=1 ;
printf( "d", x ? y ? x : y : x ) ;
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Loga
  • 39
  • 5
  • If you ask "why", you should tell us why you think it should not be like that. – Gerhardh Jun 05 '18 at 10:52
  • I am asking because i dont know . I never claimed that the print value is false or correct. – Loga Jun 05 '18 at 10:52
  • 2
    Once answers come in, it is bad form on SO to delete parts of the question. Post rolled back. – chux - Reinstate Monica Jun 05 '18 at 11:06
  • 1
    Note that for some obscure reasons, the PHP guys thought it would be a good idea to reverse the associativity of the ternary conditional operator cf. C, C++, C#, and Java. In your case though, this doesn't matter. – Bathsheba Jun 05 '18 at 11:11

7 Answers7

2

For the first one, its a "Nested" terenary operator. I would put parentheses around it to make it more decodable. Consider 0 ? 1 ? 2 : 3 : 4, Lets transform this to 0 ? (1 ? 2 : 3) : (4) is 0 ? the else part executes which is 4

For the second you are missing the %d

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
Samer Tufail
  • 1,835
  • 15
  • 25
1

Break it down with if..else statement

if(0){
    if(1)
        printf("%d\n", 2); 
    else
        printf("%d\n", 3);
} 
else
    printf("%d\n", 4);
haccks
  • 104,019
  • 25
  • 176
  • 264
1
0 ? 1 ? 2 : 3 : 4

parsed as a

(0 ? (1 ? 2 : 3) : 4)

So, you got output as a 4 .

msc
  • 33,420
  • 29
  • 119
  • 214
1

That indeed should print 4. A ternary operator works as follows:

(condition) ? expression1 : expression2

If condition evaluates to true expression1 is returned, and otherwise expression2 is returned. In your case the structure is as follows:

0?(1?2:3):4 i.e Here 1?2:3 is expression1, 4 is the expression2 and the in place of condition we have 0. As you may know 0 in a condition evaluates to false and any non-zero value evaluates to true. So here since the condition is false (i.e 0) expression2(4) is returned.

1

Ternary operators is like if else. If you add parentheses to your code, things get simpler:

0 ? (1 ? 2 : 3) : 4

Remember that in C zero means false, all non-zero means true. So above statement fails at test and returns its third part, that is 4.

user14063792468
  • 839
  • 12
  • 28
1
printf("%d", 0 ? 1 ? 2 : 3 : 4 );

Here format specifier is "%d" so it's printing the correct value that is 4. but, in

int x=0, y=1 ;
printf( "d", x ? y ? x : y : x ) ;

here no format specifier mentioned so it's just printed the "d" and ignored other parameter.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Milan
  • 179
  • 1
  • 8
0

You have multiple ternary operator in the statement printf("%d", 0 ? 1 ? 2 : 3 : 4 );

And when ever same operator comes multiple times we used to check associativity which is from Right to Left for ternary operator i.e first solves right most ternary opeartor. Or you can see this in man 1 operator

First pick right most ternary operator 1 ? 2 : 3 which results in 2. Now 3 gone.

Now it becomes 0 ? 2 : 4 which results in 4. That's why it prints 4.

Note :- As other's said, with a right associative ternary operator, you can stack them and build an if-else expression, like this:

if(0){ if(1) printf("%d\n", 2); else printf("%d\n", 3); } else printf("%d\n", 4);

Achal
  • 11,821
  • 2
  • 15
  • 37
  • Right-associativity is not relevant, because there is only one possible way to parse this. Right-associativity matters for `a?b:c?d:e` which could be parsed in two different ways. – interjay Jun 05 '18 at 11:01
  • i saw a comment on a deleted answer and he said assosiativity isnt relative in this case but only in this `a?b:c?d:e`.I m not saying u are wrong im just asking.^^^^^^^^^^^^^ – Loga Jun 05 '18 at 11:03
  • missclick.i reloaded page and it appeared on top. – Loga Jun 05 '18 at 11:04
  • It matters @interjay otherwise why one would have added in man page. – Achal Jun 05 '18 at 11:05
  • 1
    Of course it matters in some cases, I even gave an example of one such case. It doesn't matter in OP's code. – interjay Jun 05 '18 at 11:05
  • so in ur case the c?d:e will execute first and the value from this output will be the expresion 2 for the a?b:exp2?if thats the case i believe you are right in this one and its 2 diffrent things – Loga Jun 05 '18 at 11:08