Click here to Skip to main content
14,767,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I encountered a question on a website where the output of the code was to be guessed:
#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 23:57pm
v2

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:
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");
        }
    }
}
   
Comments
Siddhant Arya 22-Nov-20 5:15am
   
Thanks a lot
The format of the ternary operator is
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 :
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
Comments
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
   
please see revised solution.
Siddhant Arya 22-Nov-20 5:25am
   
Got it, thanks a lot
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
   
Comments
Siddhant Arya 22-Nov-20 5:15am
   
thanks a lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900