Conditional Operator in C

The Conditional Operator in C, also called a Ternary operator, is one of the Operators, which used in the decision-making process. The C Programming Conditional Operator returns the statement depends upon the given expression result.

The basic syntax of a Ternary Operator in C Programming is as shown below:

Test_expression ? statement1: statement2

From the above syntax, If the given test condition is true, then it returns statement1, and if it is false, statement2 will return.

Conditional Operator in C Example

In this program, we use the Conditional Operator to find whether the person is eligible to vote or not.

/* Ternary Operator in C Example */

#include<stdio.h> 

int main()
{
  int age;

  printf(" Please Enter your age here: \n ");
  scanf(" %d ", &age);

 (age >= 18) ? printf(" You are eligible to Vote ") :
               printf(" You are not eligible to Vote ");

 return 0;
}

This Ternary Operator in c program allows the user to enter his or her age and assign the user entered integer value to age variable. If the user entered value is 18 or above, the C Programming will print the first statement after the ? symbol.

printf(" You are eligible to Vote ")

If the user enters below 18, the second statement (which is after the : symbol) will print.

printf(" You are not eligible to Vote ")
Conditional Operator in C 1

Let us try with a different value to test the conditional Operator.

Please Enter your age here: 
19
You are eligible to Vote