How is the output calculated for this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How is the output calculated for this code?

int a = 10, b = 20; int c= b/a? a:b; printf("%d", c); //Please help that how it is calculated//

30th May 2021, 10:45 AM
jayraj kharadkar
jayraj kharadkar - avatar
2 Answers
+ 10
It's ternary operator. Question? answer1:answer2 So, your code should be like this int c = b/a? a:b; For a true question, it evaluates answer1 and returns its value; otherwise, it evaluates answer2 and returns its value. Here, 20/10 is 2. So, it's a true statement because any number evaluates true except zero. That's why output is 10 which is a.
30th May 2021, 11:26 AM
Simba
Simba - avatar
+ 1
Syntax error Run it in code Playground and find out yourself
30th May 2021, 11:07 AM
Benjamin Jürgens
Benjamin Jürgens - avatar