logo image

Conditional Operator (Ternary Operator) in C Language

Views: 10

You can write Ternary Operator in C Language using ?: Following example describes you where to use and how to use. Lets say you have two variables a, b and you need to assign the values a or b to another variable let say result

In this situation you can use Conditional Operator like below

int result = (expression) ? a : b;

In the above code, if expression evaluates to true then result will be assigned with a otherwise with b. For example your expression is (a > b) then you write code something like below

int result = (a>b) ? a : b;

This means result gets the max value of a and b