The Conditional Operator, also called as Ternary operator is one of the C Programming Operator, which used in decision making process. In C Programming, Conditional Operator returns the statement depends upon the given expression result. Let’s see the syntax of the conditional operator
Conditional Operator in C Syntax
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 will return statement1 and if the condition is false then statement2 is returned.
Conditional Operator in C Example
In this C Program, we are going to use the Conditional Operator to find whether the person is eligible to vote or not.
C PROGRAMMING CODE
/* Ternary Operator in C Example */
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 ");
This program allows the user to enter his or her age and assign the user entered integer value to age variable. If user entered value is 18 or above then it will print the first statement after the ? symbol.
printf(" You are eligible to Vote ")
If he enters below 18 then second statement (which is after the : symbol) will be printed.
printf(" You are not eligible to Vote ")
Let us try with different value
Thank You for Visiting Our Blog