answersLogoWhite

0


Best Answer

A ternary operator is an operator that requires three operands, as opposed to a binary operator that requires two operands and a unary operator that requires just one operand.

C++ has just one ternary operator, the conditional ternary operator:

<boolean expression> ? <expression #1> : <expression #2>;

If the boolean expression evaluates true, the first expression is evaluated, otherwise the second expression is evaluated.

A typical usage of this operator is to return the larger (or smaller) of two values of type T:

template<typename T>

T max (T a, T b) {return a<b ? b : a};

template<typename T>

T min (T a, T b) {return a<b ? a : b};

These are really nothing more than notational shorthand for the following:

template<typename T>

T max (T a, T b) {if (a<b) return b; else return a; };

template<typename T>

T min (T a, T b) {if (a<b) return a; else return b;};

However, because ternary expressions are evaluated, the return value of the expression can be used in more complex expressions:

int a=42, b=0;

// ...

int c = ((a>b ? a : b) = 1);

In the above expression, whichever is the larger of a and b will be assigned the value 1 which will also be assigned to c. Thus a and c become 1 while b remains 0.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

No. The ternary operator (known as the conditional operator in C++) cannot be overloaded because it is impossible to pass a test operand and two expression operands (either or both of which may be comma-separated) to a function. You can only pass values or references as arguments to a function. Even if it were possible, built-in functions and operators that rely on the conditional operator would likely break. Like all the other operators that cannot be overloaded (sizeof, typeid, ::, . and .*) the results must always be predictable because built-in operators and functions rely on them so heavily.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

A conditional operator (? : ) is called the ternary operator

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Which operator is called ternary operator?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the conditional operators in c language?

The conditional operator is also known as ternary operator. It is called ternary operator because it takes three arguments. The conditional operator evaluates an expression returning a value if that expression is true and different one if the expression is evaluated as false.Syntax:condition ? result1 : result2If the condition is true, result1 is returned else result2 is returned.


How do you find the greatest of three numbers using ternery operator?

Compare the first two numbers with the ternary operator. Store the result in a temporary variable. Compare the temporary variable with the third number, again using the ternary operator.


What are special operators in c plus plus?

The only "special" operators in C++ are those that cannot be overloaded. That is; the dot member operator (.), pointer to member operator (.*), ternary conditional operator (:?), scope resolution operator (::), sizeof() and typeof().


Enumerate the types of selection constructs in c plus plus?

Selection constructs in C++if...elseswitch/caseconditional ternary operator (?:)


The statement can cause other statements to execute under certain conditions?

Selection statement: if, switch/case, ternary conditional operator.

Related questions

What is the conditional operators in c language?

The conditional operator is also known as ternary operator. It is called ternary operator because it takes three arguments. The conditional operator evaluates an expression returning a value if that expression is true and different one if the expression is evaluated as false.Syntax:condition ? result1 : result2If the condition is true, result1 is returned else result2 is returned.


How do you find the greatest of three numbers using ternery operator?

Compare the first two numbers with the ternary operator. Store the result in a temporary variable. Compare the temporary variable with the third number, again using the ternary operator.


What is condional operator?

Conditional Operator- Its the only ternary operator in c/c++.- Its syntax is-(condition)?statement1:statement2;-Shruti Jain


What is the name of the expression that is formed by using logical operators?

Ternary operator


How can you download c compiler?

write a c program to fine largest/smallest of 3no (using ?:ternary operator/conditional operator)


Which c plus plus operators cannot be overloaded?

1. Member-of operator (.) 2. Pointer-to-member-of operator (.*) 3. Ternary condition operator (?:) 4. Scope resolution operator (::) 5. sizeof operator 6. typeid operator


What are special operators in c plus plus?

The only "special" operators in C++ are those that cannot be overloaded. That is; the dot member operator (.), pointer to member operator (.*), ternary conditional operator (:?), scope resolution operator (::), sizeof() and typeof().


What is ternery oparetor?

A ternary operator takes 3 operands. The only one I can think of in C# or C is the"? :" operator: ? : For example:Console.Write(1==2 ? "Huh?" : "Impossible"); //Impossible is printed


Enumerate the types of selection constructs in c plus plus?

Selection constructs in C++if...elseswitch/caseconditional ternary operator (?:)


What is the significance of ternary operator?

The ternary operator, used in C and adapted into many other programming languages, such as Java, provides a convenient shortcut for an "if" loop in many cases. Consider this example. if (age &gt;= 18) result = "old enough"; else result = "too young"; This can be written as: result = (age &gt;= 18) ? "old enough" : "too young"; For this to work, the same action must be performed in the "if" and in the "else". In this example, assign the result to the same variable. This is because the ternary operator just calculates a single result based on a condition.


The statement can cause other statements to execute under certain conditions?

Selection statement: if, switch/case, ternary conditional operator.


What are Short circuit operators in c language?

&amp;&amp; and are short circuit operator in C. It means that expressions chained with these operators are only evaluated until the result is unambiguously determined. For example, the expression a &amp;&amp; b is guaranteed to be false if a is false. In this case, the term b is not evaluated, and any possible side-effects of b will not occur. The logical OR () is implemented in a similar fashion: c d is guaranteed to be true of c evaluates to true, and d is not being evaluated in this case. The ternary ? operator is not a short circuit operator (this was listed as a short-circuit operator in a previous revision of this answer). An expression that uses the ternary operator, for example e = f ? g : h is nothing but an alternative form of an if-else construct. The terms f, g and h may each contain short-circuit operators and be evaluated in the manner discussed above, but the ternary operator itself has no short-circuit characteristic.