The ternary operator in the C programming language is the most concise way which helps in evaluate conditions and choose between two. It also makes your code cleaner as well as more efficient. Many often call this operator a conditional operator in the C programming language. In simple words, it does the work like a simple if-else in the shortest possible way.number itself. All those numbers that are not prime are called composite numbers.
With compact syntax, the ternary operator in C programming language helps in the decision making process, enhancing our readability and also in the meanwhile reducing the number of lines of code simultaneously.


Whether you are doing work of assigning values, returning results, or even handling some conditions inline, then the ternary operator is one of the essential tools for writing optimised and concise statements in the C programming language.
Ternary or this conditional operator in C acts as a compact way to evaluate the condition and return one of the two values as a result. This is a special operator used for quick decision-making in your code.
This is like a shortcut for writing simple if-else statements. Instead of writing multiple lines to check a particular condition and choose a value. Use them in one line by using the C language ternary operator.
The syntax of this ternary operator in the C language is as follows :
If this above condition is true then the first value should be chosen or else if this condition is false then the second value is chosen.
| Components | EXplanation |
| Condition | This is an expression that will either evaluate to true (non-zero) or false ( zero ) |
| ? | Just tells about the start of the ternary operator. |
| expression_if_true | The value of the expression is executed only if the condition is true. |
| : | It majorly helps in separating true expressions from false expressions. |
| expression_if_false | This will be executed when the expression is false. |
For example :
See below the Pseudocode to find whether the number is prime using a for loop :
int a = 10, b = 20;
int max = (a > b) ? a : b; // Chooses the larger number
In the above case, the C ternary operator will check if a>b. If this is true, then a will be assigned to max, and if false, then b will be assigned. This itself shows how quickly and simply it can handle conditions. Not a big long if-else block is required.
#include
int main() {
int a = 10, b = 20;
// Using the ternary operator to find the larger number
int max = (a > b) ? a : b;
printf("The larger number is: %d\n", max);
return 0;
}
Output :
The larger number is 20
Explanation: Here program will check (a > b) And if this is true, a will be assigned to max, and if false, then b will be assigned to this max.
#include
int main() {
int num = -5;
//Using the ternary operator to check if the number is positive or negative
char* result = (num >= 0)? "Positive": "Negative";
printf("The number %d is %s\n", num, result);
return 0;
}
Output :
The number -5 is Negative
Explanation - The (num >= 0) condition will check whether that number is nonnegative or not. If it is nonnegative, then positive is displayed, while if falhen negative will be displayed.
#include
int main() {
int marks = 85;
// Using ternary operator to assign a grade
char* grade = (marks >= 50) ? "Pass" "Fail";
printf("Marks: %d, Result: %s\n", marks, grade);
return 0;
}
Output :
Marks: 85, Result: Pass
Explanation:The condition (marks >= 50) will check if the marks are 50 or more per the condition defined.
If true, then it will print the result as "Pass".
If it is false, then it will print the result as "Fail".
A nested ternary operator mostly means that one ternary operator is placed inside another. This helps in handling multiple sets of conditions
Syntax for Nested Ternary Operator
condition1 ? (condition2 ? expression1 : expression2) : expression3;
Here,
#include
int main() {
int a = 15, b = 10, c = 20;
// Nested ternary operator to find the smallest number
int smallest = (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c);
printf("The smallest number is: %d\n", smallest);
return 0;
}
Output :
The smallest number is: 10
Explanation -
#include
// Function to find the larger of two numbers
int findMax(int a, int b) {
return (a > b) ? a : b; // Ternary operator to decide the return value
}
int main() {
int x = 10, y = 20;
printf("The larger number is: %d\n", findMax(x, y));
return 0;
}
Output :
The larger number is 20
Explanation :/p>
#include
int main() {
int num = 5;
// Assign result based on condition
char* result = (num % 2 == 0) ? "Even" "Odd";
printf("The number %d is %s.\n", num, result);
return 0;
}
Output :
The number 5 is Odd.
Explanation :
#include
// Function to print the larger of two numbers
void printLarger(int max) {
printf("The larger number is: %d\n", max);
}
int main() {
int a = 30, b = 25;
// Directly passing the result of the ternary operator
printLarger((a > b) ? a : b);
return 0;
}
Output :
The larger number is 300
Explanation :
#include
int main() {
int x = 10, y = 20, z = 15;
// Find the maximum of three numbers using ternary operators
int max = (x > y) ? ((x > z) ? x z) : ((y > z) ? y : z);
printf("The largest number is: %d\n", max);
return 0;
}
Output :
The largest number is: 20
Explanation :
Enroll in the C programming course in Noida And reach out to us for any queries. Make your career in the right direction and excel in the areas you want. Be ready to excel in your career
Personalized learning paths with interactive materials and progress tracking for optimal learning experience.
Explore LMSCreate professional, ATS-optimized resumes tailored for tech roles with intelligent suggestions.
Build ResumeDetailed analysis of how your resume performs in Applicant Tracking Systems with actionable insights.
Check ResumeAI analyzes your code for efficiency, best practices, and bugs with instant feedback.
Try Code ReviewPractice coding in 20+ languages with our cloud-based compiler that works on any device.
Start Coding
TRENDING
BESTSELLER
BESTSELLER
TRENDING
HOT
BESTSELLER
HOT
BESTSELLER
BESTSELLER
HOT
POPULAR