What is the output of this line in C... int c = (a < b) ? b : a ; | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the output of this line in C... int c = (a < b) ? b : a ;

How it will be executed??

15th Apr 2019, 3:57 PM
T Harish Kumar
T Harish Kumar - avatar
1 Answer
+ 2
This is called a ternary operator. int c -> we will assign a value to c (a < b) ? -> the question mark indicates we will assign c to a value depending on the outcome of the preceeding boolean condition (in this case a < b) The first value after the ? is what to assign to c if the boolean condition is true. The value after the : is what to assign if the boolean condition is false. Read ternary operators like an if-else assignment in one line. So... int c will be the value of b if (a < b) else it will the value of a Also the same as int c if(a < b) { c = b; } else { c = a; } Hope that makes sense?
15th Apr 2019, 6:37 PM
Jenine