Conditional Operators in C Programming

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 and it takes three arguments the 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.

#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

Sample Conditional Operator C Programs

C program to find maximum of two numbers using conditional operator
C program to find maximum of three numbers using conditional operator
C program to check a number is odd or even using conditional operator
C program to check decimal digit character using conditional operator