Ternary Operator in C: Syntax, Examples and Advantages

Ternary Operator in C: Syntax, Examples and Advantages

3 mins read2.4K Views Comment
Updated on Jan 30, 2023 17:30 IST

The Ternary operator in C is a shorthand way to write an if-else statement. The ternary operator in C makes the code more compact and easier to understand, which is important for programmers who are concerned with the size and readability of the code.

2023_01_MicrosoftTeams-image-134.jpg

In this article, we will learn about the ternary operator and how to implement it in C, with examples. We will be covering the following sections:

So, without further ado, let’s get started!

Ternary Operator in C

The ternary operator, also known as the conditional operator, is a powerful tool in C programming. The ternary operator takes three arguments: a conditional expression, a result if the condition is True, and a result if the condition is False. The ternary operator is represented by the “? :” symbol and can be used to simplify and make your code more readable and efficient.

Explore free C programming courses

Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

4 K
2 months
– / –
6 months
– / –
1 month
3.5 K
3 months
15 K
2 months
– / –
50 hours
– / –
40 hours
– / –
2 months
– / –
4 months
5.5 K
140 hours

Ternary Operator Syntax

Here’s the syntax for a ternary operator in C:

condition ? true_val : false_val

2023_01_Ternary-Operator-in-C-image-1.jpg

The three operands in a ternary operator include:

  • condition: A Boolean expression that evaluates whether it is True or False.
  • true_val: Executes the true_val statement if the condition is True.
  • false_val: Execute the false_val statement if the condition is False.

Now, let’s look at the implementation of the ternary operator in C programming:

Examples of Ternary Operator in C

Example 1:

Let us consider an example to check which of the two input numbers is greater using the ternary operator in C:


 
#include
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
// Using the ternary operator to check which number is greater
(num1 > num2) ? printf("%d is greater than %d\n", num1, num2) : printf("%d is greater than %d\n", num2, num1);
return 0;
}
Copy code

Output 1:


 
Enter two numbers: 23
34
34 is greater than 23
Copy code

In this program, we are using the ternary operator to check if the value of num1 is greater than num2 or not. If the condition (num1 > num2) is true, the operator returns the value of the expression before the colon (:). Otherwise, it returns the value of the expression after the colon.

Example 2:

Let us consider another example to check if a person is a minor or an adult using the ternary operators:


 
#include
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
// Using the ternary operator to check if the person is a minor or not
const char* status = (age < 18) ? "minor" : "adult";
printf("You are: %s\n", status);
return 0;
}
Copy code

Output 2:


 
Enter your age: 20
You are: adult
Copy code

In this example, the program prompts the user to enter their age, then it uses the ternary operator to check if the person is a minor (age < 18) or an adult. If the condition is true, the operator assigns the value “minor” to the variable status, otherwise, it assigns the value “adult” to the variable status. Finally, the program prints the value of the variable status.

The above program could also be written using the if-else statement as shown below:


 
#include
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
const char* status;
if(age < 18)
status = "minor";
else
status = "adult";
printf("You are: %s\n", status);
return 0;
}
Copy code

Output:


 
Enter your age: 16
You are: minor
Copy code

As you can see, the ternary operator in this example allows us to condense the if-else statement into a single line, making the code more compact and easier to read.

Understanding Operators in C++
New and Delete Operators in C++
Operator Overloading in C++

Advantages of Ternary Operators Over If-Else Statements

Ternary operators offer two main benefits over traditional if-else statements:

  • They allow for more compact and efficient code.
  • They can improve code readability by reducing the number of lines needed.

However, whether or not to use ternary operators is ultimately a matter of personal preference and coding style.

Explore free online courses with certificates

Endnotes

Hope this article was helpful for you to understand ternary operators in C Programming. We learned how these operators are used to replace the if-else conditions in our C program code, thereby making it more efficient and readable. Explore our C articles to find out more about the language and consolidate your knowledge of the fundamentals.

Explore programming courses

Recently completed any professional course/certification from the market? Tell us what you liked or disliked in the course for more curated content.

Click here to submit its review.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio