Lesson 21 of 60 – Conditional Operator in C
35%

Conditional Operator in C

The conditional operator is a special operator in C that provides a short way to choose one of two expressions based on a condition. It is also called the ternary operator because it works with three operands.

Note: The conditional operator uses the syntax condition ? expression1 : expression2. If the condition is true, expression1 is selected. Otherwise, expression2 is selected.

1. What is the Conditional Operator?

The conditional operator allows a program to select one of two expressions depending on whether a condition is true or false.

int age = 20;

char *result = (age >= 18) ? "Adult" : "Minor";

printf("%s", result);

Output:

Adult

2. Why is it Called Ternary Operator?

The conditional operator is called the ternary operator because it operates on three operands:

  1. A condition
  2. The expression selected when the condition is true
  3. The expression selected when the condition is false
condition ? true_expression : false_expression;

3. Syntax of Conditional Operator

condition ? expression1 : expression2;

The question mark ? separates the condition from the true expression, while the colon : separates the true and false expressions.

4. How the Conditional Operator Works

int number = 10;

int result = (number > 5) ? 100 : 200;

The condition number > 5 is true, so 100 is selected and assigned to result.

result = 100

5. Simple Conditional Example

int age = 20;

int result = (age >= 18) ? 1 : 0;

printf("%d", result);

Output:

1

Because the age is at least 18, the first expression is selected.

6. Conditional Operator with Two Numbers

int a = 10;
int b = 20;

int larger = (a > b) ? a : b;

printf("%d", larger);

Output:

20

The operator selects the larger value.

7. Finding the Smaller Number

int a = 10;
int b = 20;

int smaller = (a < b) ? a : b;

printf("%d", smaller);

Output:

10

8. Conditional Operator with Strings

int marks = 75;

char *result = (marks >= 40) ? "Pass" : "Fail";

printf("%s", result);

Output:

Pass

The conditional operator selects one of the two string literals.

9. Checking Even or Odd

int number = 7;

char *result = (number % 2 == 0) ? "Even" : "Odd";

printf("%s", result);

Output:

Odd

The remainder operator is used to create the condition.

10. Checking Positive or Negative

int number = -5;

char *result = (number >= 0) ? "Positive" : "Negative";

printf("%s", result);

Output:

Negative

This example treats zero as non-negative.

11. Conditional Operator with Assignment

int age = 25;

int status;

status = (age >= 18) ? 1 : 0;

printf("%d", status);

Output:

1

12. Conditional Operator with printf()

int age = 16;

printf("%s", (age >= 18) ? "Adult" : "Minor");

Output:

Minor

The selected expression is passed directly to printf().

13. Conditional Operator vs if-else

Using if-else:

if (age >= 18)
{
    result = 1;
}
else
{
    result = 0;
}

Using the conditional operator:

result = (age >= 18) ? 1 : 0;

The conditional operator can make a simple two-choice assignment more compact.

14. Conditional Operator with User Input

#include <stdio.h>

int main()
{
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

    printf("%s", (number % 2 == 0) ? "Even" : "Odd");

    return 0;
}

The program takes a number and directly displays whether it is even or odd.

15. Checking Pass or Fail

#include <stdio.h>

int main()
{
    int marks;

    printf("Enter marks: ");
    scanf("%d", &marks);

    printf("%s", (marks >= 40) ? "Pass" : "Fail");

    return 0;
}

The condition checks whether the marks are at least 40.

16. Finding the Maximum of Two Values

#include <stdio.h>

int main()
{
    int a = 50;
    int b = 80;

    int max = (a > b) ? a : b;

    printf("Maximum = %d", max);

    return 0;
}

Output:

Maximum = 80

17. Finding the Minimum of Two Values

#include <stdio.h>

int main()
{
    int a = 50;
    int b = 80;

    int min = (a < b) ? a : b;

    printf("Minimum = %d", min);

    return 0;
}

Output:

Minimum = 50

18. Conditional Operator with Floating-Point Values

float a = 10.5f;
float b = 20.5f;

float result = (a > b) ? a : b;

printf("%.2f", result);

Output:

20.50

The conditional operator can also select between floating-point values.

19. Conditional Operator with Characters

char grade1 = 'A';
char grade2 = 'B';

char result = (grade1 < grade2) ? grade1 : grade2;

printf("%c", result);

The operator can select one of two compatible character values.

20. Nested Conditional Operator

A conditional operator can be placed inside another conditional expression. This is called a nested conditional operator.

int marks = 75;

char *grade =
    (marks >= 80) ? "A" :
    (marks >= 60) ? "B" :
    (marks >= 40) ? "C" : "F";

printf("%s", grade);

Output:

B

Nested conditional expressions should be used carefully because too many levels can make code difficult to read.

21. Multiple Conditions

int age = 25;
int marks = 70;

char *result =
    (age >= 18 && marks >= 40) ? "Eligible" : "Not Eligible";

printf("%s", result);

Logical and relational operators can be used inside the condition.

22. Conditional Operator with Zero

int value = 0;

char *result = value ? "True" : "False";

printf("%s", result);

Output:

False

In a condition, zero is treated as false.

23. Conditional Operator with Nonzero Values

int value = 10;

char *result = value ? "True" : "False";

printf("%s", result);

Output:

True

In a condition, a nonzero value is treated as true.

24. Conditional Operator in Calculations

int price = 500;
int discount = 1;

int finalPrice = discount ? price - 50 : price;

printf("Final Price = %d", finalPrice);

Output:

Final Price = 450

The selected expression becomes the value assigned to finalPrice.

25. Conditional Operator for Absolute Value

int number = -15;

int absolute = (number < 0) ? -number : number;

printf("%d", absolute);

Output:

15

The operator chooses the appropriate expression based on whether the number is negative.

26. Common Mistakes

  • Forgetting the ? symbol.
  • Forgetting the : symbol.
  • Using more than two result expressions without proper nesting.
  • Confusing ? with other operators.
  • Making nested conditional expressions too complicated.
  • Using expressions with incompatible types without understanding the resulting type.
  • Using the conditional operator where a longer if-else statement would be clearer.

27. Conditional Operator and Parentheses

int a = 10;
int b = 20;

int result = (a > b) ? a : b;

printf("%d", result);

Parentheses around the condition are commonly used to make the condition easy to read.

The entire conditional expression can also be placed inside a larger expression when appropriate.

28. Complete Conditional Operator Program

#include <stdio.h>

int main()
{
    int a, b;

    printf("Enter first number: ");
    scanf("%d", &a);

    printf("Enter second number: ");
    scanf("%d", &b);

    int maximum = (a > b) ? a : b;
    int minimum = (a < b) ? a : b;

    printf("Maximum = %d\n", maximum);
    printf("Minimum = %d\n", minimum);

    return 0;
}

This program uses the conditional operator to find both the maximum and minimum of two numbers.

29. Conditional Operator vs if-else Example

Using if-else:

int number = 10;
int result;

if (number > 0)
{
    result = 1;
}
else
{
    result = 0;
}

Using conditional operator:

int number = 10;

int result = (number > 0) ? 1 : 0;

Both approaches can produce the same result. The conditional operator is particularly useful for short expressions with two alternatives.

30. Practice with Conditional Operator

Practice the following programs:

  1. Check whether a number is even or odd.
  2. Check whether a number is positive or negative.
  3. Find the larger of two numbers.
  4. Find the smaller of two numbers.
  5. Check whether a student has passed.
  6. Check whether a person is an adult or minor.
  7. Find the maximum of two values.
  8. Find the minimum of two values.
  9. Calculate a discounted price using the conditional operator.
  10. Create a grade system using nested conditional operators.

Practice simple conditions first. Once you understand the basic syntax, you can use the conditional operator with calculations and multiple conditions.

📌 Key Points

  • The conditional operator is also called the ternary operator.
  • Its syntax is condition ? expression1 : expression2.
  • It works with three operands.
  • The first expression is selected when the condition is true.
  • The second expression is selected when the condition is false.
  • It is useful for simple two-choice expressions.
  • It can be used with numbers, characters, strings, and other compatible expressions.
  • It can be combined with relational and logical operators.
  • Nested conditional operators are possible but should be kept readable.
  • For complex decision-making, a normal if-else statement may be clearer.

🧠 Quick Quiz

Question: Which operator is known as the ternary or conditional operator in C?