GET EDUCATE

Contents
Basics of C Programming

⯈Basic C Programs

Conditional operator in C (?:)

  • It is a ternary operator.
  • The operator is ?:
  • It acts on three operands to give the answer.

The syntax for using the ternary operator is

y = a? b : c;

where ‘a’, ‘b’ and ‘c’ are operands.

y = b, if ‘a’ is TRUE.

y = c, if ‘a’ is FALSE. 

You may also refer to the video posted at the end of this page.

Examples:


y = (4>2) ? 4 : 2;

Evaluate (4>2), since 4>2 is Ture, we take the value written in between ? and : and that is 4. 

So, y = 4


y = 0==0 ? 1 : 0;

Evaluate (0==0), since 0==0 is Ture, we take the value written in between ? and : and that is 1. 

So, y = 1


y = 4!=4? 1 : 0

Evaluate (4!=4), since 4!=4 is False, we take the value written after “:” and that is 0. 

So, y = 0

Scroll to Top