
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
What is ternary operator (? X : Y) in C++?
The conditional operator (? :) is a ternary operator (it takes three operands). The conditional operator works as follows −
- The first operand is implicitly converted to bool. It is evaluated and all side effects are completed before continuing.
- If the first operand evaluates to true (1), the second operand is evaluated.
- If the first operand evaluates to false (0), the third operand is evaluated.
- The result of the conditional operator is the result of whichever operand is evaluated — the second or the third. Only one of the last two operands is evaluated in a conditional expression. The evaluation of the conditional operator is very complex. The steps above were just a quick intro to it. Conditional expressions have right-to-left associativity. The first operand must be of integral or pointer type.
- The following rules apply to the second and third operands −
- If both operands are of the same type, the result is of that type.
- If both operands are of arithmetic or enumeration types, the usual arithmetic
- conversions (covered in Standard Conversions) are performed to convert them to a common type.
- If both operands are of pointer types or if one is a pointer type and the other is a constant expression that evaluates to 0, pointer conversions are performed to convert them to a common type.
- If both operands are of reference types, reference conversions are performed to convert them to a common type.
- If both operands are of type void, the common type is type void.
- If both operands are of the same user-defined type, the common type is that type.
- If the operands have different types and at least one of the operands has user-defined type then the language rules are used to determine the common type. (See warning below.)
example
#include <iostream> using namespace std; int main() { int i = 1, j = 2; cout << ( i > j ? i : j ) << " is greater." << endl; }
Output
This will give the output −
2 is greater.
- Related Articles
- What is ternary operator in C#?
- What is a Ternary operator/conditional operator in C#?
- What is a ternary operator (?:) in JavaScript?
- Ternary Operator in Java
- Ternary Operator in Python?
- Ternary Operator in C#
- Changing ternary operator into non-ternary - JavaScript?
- Conditional ternary operator ( ?: ) in C++
- Ternary Operator in Dart Programming
- C/C++ Ternary Operator
- Java Ternary Operator Examples
- How Ternary operator in PowerShell Works?
- Is there an equivalent of C’s “?:” ternary operator in Python?
- How to overload python ternary operator?
- Difference between the Ternary operator and Null coalescing operator in php

Advertisements