15,613,716 members
See more:
I encountered a question on a website where the output of the code was to be guessed:
C
```#include <stdio.h>

int main()
{
int a=3,b=3,c=3;
a>b? a>c?printf("a"):a==c?printf("a c"):printf("c"):
b>c?printf("b"):b==c?printf("b c"):
b==a?printf("a b"):printf("b");

return 0;
}```

What I have tried:

By checking each individual condition of the ternary I was unable to point out the output, when I tried to run the code on console, this is what i got:
`b c`

Totally unaware of how the output is generated, can anyone please explain me the output.
Thanks a lot.
Posted
Updated 21-Nov-20 22:57pm
v2

## Solution 3

C++
```int a=3,b=3,c=3;
a>b? a>c?printf("a"):a==c?printf("a c"):printf("c"):
b>c?printf("b"):b==c?printf("b c"):
b==a?printf("a b"):printf("b");
```

The equivalent code is:
C++
```int a=3,b=3,c=3;
if (a > b)
{
if (a > c)
printf("a");
else
{
if (a == c)
printf("a c");
else
printf("c");
}
else
{
if (b > c)
printf("b");
else
{
if (b == c)
printf("b c");
else
{
if (b == a)
printf("a b");
else
printf("b");
}
}
}```

Siddhant Arya 22-Nov-20 5:15am
Thanks a lot

## Solution 1

The format of the ternary operator is
C++
`condition ? true_statement : false_statement;`
which means when `condition` is true the `true_statement` is executed and when it is false the `false_statement` is executed.

It will probably help to rewrite the expression so you can see the true and false statements of each subexpression :
C++
```a > b
? a > c
? printf("a")
: a == c
? printf("a c")
: printf("c")
: b > c
? printf("b")
: b == c
? printf("b c")
: b == a
? printf("a b")
: printf("b");```

v2
Siddhant Arya 22-Nov-20 4:54am
I am aware of the syntax, but unable to trace how I am getting the above output. It would be helpful if you can explain😊😊
Rick York 22-Nov-20 5:17am
Siddhant Arya 22-Nov-20 5:25am
Got it, thanks a lot

## Solution 2

Quote:
By checking each individual condition of the ternary I was unable to point out the output

The code is specially crafted to make its structure difficult or impossible to figure out.
You need to rewrite and reformat the code.
The rewriting is not exactly C for syntax reasons.
```Example on last line:
b==a?printf("a b"):printf("b");

- Write an "if" before each condition
- replace each "?" with a "then"
- replace each ":" with a "else"
if b==a then printf("a b") else printf("b");

- make new line everywhere needed
if b==a then
printf("a b")
else
printf("b");

- indent the code
if b==a then
printf("a b")
else
printf("b");

- clear syntax as you need```