what is ternary operator in c

what is ternary operator in c

1 year ago 84
Nature

The ternary operator in C is a way to shorten the simple if-else code block in C/C++ . It is also known as the conditional operator and takes three operands or variables, unlike other operators which take one or two operands. The ternary operator in C allows us to execute different code depending on the first argument, i.e. based on condition. The syntax of the ternary operator in C is: testCondition ? expression1 : expression2; . The testCondition is a boolean expression that results in either true or false. If the condition is true, expression1 (before the colon) is executed, and if it is false, expression2 (after the colon) is executed. The ternary operator is used to minimize code size and improve compiler efficiency. It can be assigned to a variable in C programming. The ternary operator is a class of conditional operators and is used for decision making in place of longer if and else conditional statements.

Read Entire Article