Press "Enter" to skip to content

Conditional or Ternary Operator in C

Conditional or Ternary Operator in C

 Program Abstraction:      This tutorial describes the Conditional or Ternary Operator in C with examples.

Conditional operator is an important operator among other operators in C and frequently asking question in exams and interviews. Conditional operator is also known as Ternary operator.

Conditional operator is related to Simple if condition statement. If the condition is true, a statement will work and if the condition is false, another statement will work. That is, the conditional operator contains a if part and an else part.

It is simple and easy if you remember its syntax.

Syntax: condtion ? statement 1 : statemen 2;

where statement 1 is ‘if part’ and statement 2 is the ‘else part’.
(Don’t forget question symbol (?) and column symbol (:) in between them).

Let’s learn with an example:

void main()
{
int a = 10;
int b = 20;
(a>b) ? printf(“a is greater”) : printf(“b is greater”);
}

Here if the condition a>b is true (the value of a greater than b), then the statement 1 [printf(“a is grater”)]will work, that is the statement after ? symbol.

If the condition a>b is false (the value of a less than b), then the statement 2 [printf(“b is greater”)] will work, that is the statement after : symbol.

working for conditional ternary operator in c

Here the value of b (20) is greater than value of (10). So statement 2 will work. So output should be : b is greater.

Sample Output:

working for conditional ternary operator output

Example:  Accept a number and check whether the number is positive or negative using conditional operator. 

#include<stdio.h>
void main()
{
int n;
printf(“Enter a number : “);
scanf(“%d”,&n);
(n>0) ? printf(“Positive”) : printf(“Negative”);
  }

Code Explanation:

How can we identify a number is positive or negative? If a number is greater than zero (>0) then number should be positive else negative. That is the principle of this program code. Here zero is an exceptional case.

Sample Output:

check whether a number is postive or negative using conditional operator

Example 2:  Program to accept two numbers, and find square of the largest number.

#include<stdio.h>
void main()
{
int a,b,big,sqr;
printf(“Enter two numbers : “);
scanf(“%d %d”,&a,&b);
big = (a>b) ? a : b;
sqr = big * big;
printf(“Square of largest number %d is %d”,big,sqr);
}

Code explanation:

In line big = (a>b) ? a : b;

If the condition (a>b) is true, then the value of a will assigned to the variable big. If the condition is false, then the value of b will assigned to the variable big. So the variable big gets the biggest among given two numbers.
conditional ternary operator in c

Sample Output:

squar of larges number using conditional operator

Example 3: Program to accept two numbers. If first number is smaller than second number, then find their sum. If the first number is larger than second number then find their difference.

#include<stdio.h>
void main()
{
int a,b,answr;
printf(“Enter two numbers : “);
scanf(“%d %d”,&a,&b);
answr = (a<b) ? a+b : a-b;
printf(“Answer is %d”,answr);
}

Sample Output:

find large number among two number

Example 4: Program to accept a number, if the number is even number, display ‘Even Number’, else display ‘Odd Number’.

#include<stdio.h>
void main()
{
int num;
printf(“Enter a number : “);
scanf(“%d”,&num);
(num%2==0) ? printf(“Even Number”) : printf(“Odd Number”);
}

Code Explanation:

We already learnt % operator in a previous lesson. num%2 == 0 means the given number divides by 2 and gets the reminder zero (0). In even numbers, if we divide by 2, then their reminder should be zero. If the reminder is one, then it should be an odd number.

Sample Output:

check whether the given number is odd or even

Be First to Comment

Leave a Reply