Conditional operators in c – Conditional Operators in C Programming

Conditional operators in c: Conditional Operator in C is a powerful Operator which can be used to implement if-then-else type of logic. This operator is also known as ternary operator as it takes three expressions in following form.

Conditional_Expression ? Expression_One : Expression_Two;
Ternary Operator will execute Expression_One if Conditional_Expression is true, otherwise it execute Expression_Two.
Ternary Operator is similar to if-else decision block as it evaluates only one code block depending on the result of Conditional_Expression

For Example
int X = 25;
int Y = (X > 20 ? 1 : 2);
As X > 20, So after above statement Y’s value becomes 1.

C Program to find larger number using Conditional Operator

Below program takes two numbers as input from user and stores them in integer variable ‘A’ and ‘B’. Then using ternary operator it prints whether A >= B or A < B.

Conditional Operators in C Programming

#include<stdio.h>
#include<conio.h>
 
int main(){
 
    int A, B;
     
    printf("Enter two numbers\n");
    scanf("%d %d", &A, &B);
     
    (A>=B) ? printf("%d is >= %d\n",A,B) : printf("%d is < %d\n",A,B);
     
    getch();
    return(0);
}

Output

5 4
5 is >= 4