Top 5 Examples of Ternary Operator in C

Top 5 Examples of Ternary Operator in C
Techiio-author
Written by Shuvhojit DebFebruary 9, 2022
11 min read
C
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

Today we'll discuss the ternary operator in C.

Introduction:

In the C language ternary driver is allowing for executing or running any law grounded on the given value to the condition, and the condition result value of the expression returned to the affair. The important use of a ternary driver decreases the number of lines of law and increases the performance of the operation. Utmost of the exploration papers claimed that the expression result is faster than a statement result (conventional if-additional condition). Ternary driver contains 3 expressions; Expression1, Expression2, and Expression3. Expression1 holds the condition to check, Expression2 will hold true value estimated by Expression1 condition, and Expression3 will hold the false value estimated by Expression1 condition.

Working of a ternary operator 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 a better understanding of ternary operator:-

blogpost

Some Examples of Ternary Operator in C:

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:-

blogpost

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:-

blogpost

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:-

blogpost

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:-

blogpost

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:-

blogpost

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 driver in C is used to reduce law and increases the compiler performance. It has condition followed by a question mark, the expression for true condition value followed by color(:) and one further expression for false condition value.

C Language
Programming
Ternary Operator
Operator
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance