 
 Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Ternary Operators in C/C++
The operators, which require three operands to act upon, are known as ternary operators. It can be represented by “ ? : ”. It is also known as conditional operator. The operator improves the performance and reduces the line of code.
Here is the syntax of ternary operator in C language,
Expression1 ? Expression2 : Expression3
Here is an example of Ternary Operators in C language,
Example
#include <stdio.h>
int main() {
   int a = -1;
   double b = 26.4231;
   int c = a? printf("True value : %lf",b):printf("False value : 0");
   return 0;
}
Output
True value : 26.423100
Expression1 will evaluate always while expression2 & expression3 are dependent on the outcome of expression1. If the outcome of expression1 is non-zero or negative, expression2 will display, otherwise expression3 will display.
The ternary operator has a return type. The return type depends on expression2 and also on the convertibility of expression3 to expression2. If they are not convertible, the compiler will throw an error.
Here is another example of ternary operator in C language,
Example
#include <stdio.h>
int main() {
   int x = -1, y = 3;
   double b = x+y+0.5;
   int c = x<y? printf("True value : %lf",b):printf("False value : 0");
   return 0;
}
Output
True value : 2.500000
