Getting Started With C

?: Operator

The conditional operator in C is kind of similar to the if-else statement as it follows the same algorithm as of if-else statement but the conditional operator takes less space and helps to write the if-else statements in the shortest way possible. It is also known as the ternary operator in C as it operates on three operands.

Syntax of Conditional/Ternary Operator in C :

The conditional operator can be in the form
variable = Expression1 ? Expression2 : Expression3;

Or the syntax can also be in this form

variable = (condition) ? Expression2 : Expression3;

Or syntax can also be in this form

(condition) ? (variable = Expression2) : (variable = Expression3);

Working of Conditional/Ternary Operator in C

Sample code:

Program.c

output:

output.c

Resources :

Featured
Special title treatment

With supporting text below as a natural lead-in to additional content.

Q1. The conditional operator are also known as

Q2. What will be the value of y if x = 8?

 y = (x  > 6 ? 4 : 6);