Ternary Operator in C: Syntax, Examples and Advantages

Ternary Operator in C: Syntax, Examples and Advantages

3 mins read2.4K Views Comment
clickHere
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:

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

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
\n \n <stdio.h>\n \n
\n \n
\n \n
int main() {\n \n
int num1, num2;\n \n
printf("Enter two numbers: ");\n \n
scanf("%d %d", &num1, &num2);\n \n
\n \n
// Using the ternary operator to check which number is greater\n \n
(num1 > num2) ? printf("%d is greater than %d\n", num1, num2) : printf("%d is greater than %d\n", num2, num1);\n \n
\n \n
return 0;\n \n
}\n \n
\n \n </stdio.h>
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
\n \n <stdio.h>\n \n
\n \n
\n \n
int main() {\n \n
int age;\n \n
printf("Enter your age: ");\n \n
scanf("%d", &age);\n \n
\n \n
// Using the ternary operator to check if the person is a minor or not\n \n
const char* status = (age < 18) ? "minor" : "adult";\n \n
printf("You are: %s\n", status);\n \n
return 0;\n \n
}\n \n
\n \n </stdio.h>
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
\n \n <stdio.h>\n \n
\n \n
\n \n
int main() {\n \n
int age;\n \n
printf("Enter your age: ");\n \n
scanf("%d", &age);\n \n
\n \n
const char* status;\n \n
if(age < 18)\n \n
status = "minor";\n \n
else\n \n
status = "adult";\n \n
printf("You are: %s\n", status);\n \n
return 0;\n \n
}\n \n
\n \n </stdio.h>
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++
Understanding Operators in C++
Operators in C++perform mathematical and logical operations on entities or variables that are known as operands. The operators are represented by symbols that allow calculation between entities to produce relevant...read more
New and Delete Operators in C++
New and Delete Operators in C++
The new operator is used to dynamically allocate memory on the heap for an object or an array of objects. And delete operator is used to deallocate the memory. This...read more
Operator Overloading in C++
Operator Overloading in C++
In C++, you can alter the way operators work for user-defined types like objects and structures. This is known as operator overloading. It is one of the best features used to...read more

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