Updated April 6, 2023
Introduction to Ternary Operator in C
In the C language ternary operator is allowing for executing or running any code based on the given value to the condition, and the condition result value of the expression returned to the output. The important use of a ternary operator decreases the number of lines of code and increases the performance of the application. Most of the research articles claimed that the expression result is faster than a statement result (conventional if-else condition). Ternary operator contains 3 expressions; Expression1, Expression2, and Expression3. Expression1 holds the condition to check, Expression2 will hold true value evaluated by Expression1 condition, and Expression3 will hold false value evaluated by Expression1 condition.
Advantages:
- It reduces the code.
- Improves performance.
- Overcome conventional use of if and else condition always.
How does ternary operator work in C language?
C language ternary operator works based on the ternary operator(?), If the condition is evaluated true then it executes the true expression value at the left-hand side of the colon(:) symbol and if the condition is evaluated false then it executes false expression value at the right-hand side of the colon(:) symbol.
Syntax:
Expression1?Expression2:Expression3;
Or
Condition?true value:false value;
What is the return value of the ternary expression?
Boolean result= Condition?true value:false value;
It returns the Boolean value(true/false).
See the below images for better understanding of ternary operator:
Examples of Ternary Operator in C
Here are the following examples mention below
Example #1
Larger number without ternary operator
Code:
#include<stdio.h>//informing to c language include c ibrary files
int main()//main method
{
//declaraing two variables
int first,second;
//printing output
printf("Please provide 2 numbers=>\n");
scanf("%d %d", &first , &second);//%d is integer values
if(first>second)//checking if condition
{
printf("%d",first);//printing the larger output
printf(" is larger number than %d",second);
}
else
{
printf("%d",second);
printf(" is larger number than %d",first);
}
return 0;
}
Output:
Example #2
The largest number from 2 numbers with the ternary operator
Code:
#include<stdio.h>//line1
main()//line2
{
int first,second,largest;//line3
printf("Please provide 2 numbers=>\n");//lin4
scanf("%d %d", &first , &second);//line5
largest = (first > second) ? first : second;//line6
printf("%d", largest);//line7
printf(" is the largest number from %d and %d",first,second);
}
Output:
Explanation:
- Line1 includes required library files to run the C language application
- Line2 is the main method where the application starts from this main () method.
- Line3 is an integer variable declaration for storing integer numbers (non-decimal numbers).
- Line4 is the asking user to enter 2 numbers text.
- Line5 is stored in the entered 2 integer numbers within the scanf method with %d operator.
- Line6 is the ternary operator compares to 2 numbers which is largest.
- Line7 printing the output on the console.
Example #3
Largest numbers from 3 numbers with the ternary operator
Code:
#include<stdio.h>//line1
int main()//line2
{
int firstNumber, secondNumber, thirdNumber, largest;//line3
printf("Enter any 3 numbers\n");//line4
scanf("%d %d %d", &firstNumber, &secondNumber, &thirdNumber);//line5
largest= (firstNumber > secondNumber) ? (firstNumber > secondNumber ? firstNumber : thirdNumber) : (secondNumber > thirdNumber ? secondNumber : thirdNumber);//line5
printf ("%d", largest);//line6
printf (" is the largest number from %d, %d and %d",firstNumber,secondNumber,thirdNumber);//line7
}
Output:
Explanation:
- Line1 includes required library files to run the C language application
- Line2 is the main method where the application starts from this main () method.
- Line3 is an integer variable declaration for storing 3 integer numbers (non-decimal numbers).
- Line4 is the asking user to enter 3 numbers text.
- Line5 is stored in the entered 3 integer numbers within the scanf method with the %d operator.
- Line6 is the ternary operator compares to 3 numbers which is largest.
- Line7 printing the output on the console.
Example #4
Decimal Smallest number and smallest number as the radius
Code:
#include<stdio.h>//line1
float getMyCircleArea (float radius);//lin2
int main()//line3
{
float x,y,smallest;//line4
printf("Please provide 2 numbers=>\n");//line5
scanf("%f %f", &x , &y);//line6
smallest = (x < y) ? x : y;//line7
printf("%f", smallest);//line8
printf(" is the smallest number from %f and %f \n",x,y);//line9
float circleArea= getMyCircleArea (smallest);//line10
printf("Circle area from smallest number as radius is=%f", circleArea);//line11
}
float getMyCircleArea (float radius)//lin12
{
float result=3.14*radius*radius;//line13
return (result);//lin14
}
Output:
Explanation:
- Line1 includes required library files to run the C language application.
- Line2 importing the user-defined method, here getMyCircleArea() is the method.
- Line3 is the main method where the application starts from this main () method.
- Line4 is a float variable declaration for storing 2 float numbers (non-decimal numbers).
- Line5 is the asking user to enter 2 decimal numbers text.
- Line6 is stored in the entered 2 decimal numbers within the scanf method with %f operator.
- Line7 is the ternary operator compares to 2 decimal numbers which is the smallest.
- Line8 and Line9 are used to print the smallest decimal number.
- Line10 is calling getMyCircleArea() with smallest number passed as radius.
- Line11 is printing the result of the circle area with the smallest number as the radius.
- Line12 is getMyCicleArea() method declaration.
- Line13 is circle area logic.
- Line14 is returning resultant value from the getMyCirlceArea() method.
Example #5
Largest number with Factorial
Code:
#include<stdio.h>//line1
int getMyFactorial(int l);//lin2
int main()//line3
{
int x,y,largest;//line4
printf("Please provide 2 numbers=>\n");//line5
scanf("%d %d", &x , &y);//line6
largest = (x > y) ? x : y;//line7
printf("%d", largest);//line8
printf(" is the largest number from %d and %d \n",x,y);//line9
int factorial=getMyFactorial(largest);//line10
printf("Factorial of the largest number is=%d", factorial);//line11
}
int getMyFactorial(int l)//lin12
{
int f=1;
for (int p = 1; p <= l; p++){
f = f * p;//line13
}
return (f);//lin14
}
Output:
Explanation:
- Line1 includes required library files to run the C language application.
- Line2 importing the user-defined method, here getMyFactorial() is the method.
- Line3 is the main method where the application starts from this main () method.
- Line4 is an integer variable declaration for storing 2 integer numbers (non-decimal numbers).
- Line5 is the asking user to enter 2 integer numbers text.
- Line6 is stored in the entered 2 integer numbers within the scanf method with %d operator.
- Line7 is the ternary operator compares to 2 integer numbers which is largest.
- Line8 and Line9 are used to print the largest integer number.
- Line10 is calling getMyFactorial () with the largest number passed as the argument.
- Line11 is printing the result of the circle area with the largest number as an argument.
- Line12 is getMyFactorial () method declaration.
- Line13 is factorial logic.
- Line14 is returning the resultant value from the getMyFactorial () method.
Conclusion
The ternary operator in C is used to reduce code and increases the compiler performance. It has condition followed by a question mark, the expression for true condition value followed by color(:) and one more expression for false condition value.
Recommended Articles
This is a guide to Ternary Operator in C. Here we discuss an introduction to ternary operators along with the working and examples for better understanding. You may also look at the following articles to learn more –