Home > Software engineering >  what is the output of conditional operator with unary operator
what is the output of conditional operator with unary operator

Time:03-19

I have the following code where behavior is not clear to me. Can some one please help how conditional operator evaluate the following code and output ans as 1

#include

int main() {

bool delayMessages=0;
bool Delay = false;
delayMessages  = Delay ? 1 : -1;
std::cout << "Hello world!"<<delayMessages;

return 0;
} 

Ans: Hello world!1

Can soemone please help how thsi code is evaluated "delayMessages = Delay ? 1 : -1;"

CodePudding user response:

From the C 17 Standard (7.6 Integral promotions)

6 A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true becoming one.

and (7.14 Boolean conversions)

1 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true. For direct-initialization (11.6), a prvalue of type std::nullptr_t can be converted to a prvalue of type bool; the resulting value is false.

and at last (8.7 Additive operators)

1 The additive operators and - group left-to-right. The usual arithmetic conversions are performed for operands of arithmetic or enumeration type.

This expression statement

delayMessages  = Delay ? 1 : -1;

can be rewritten like

delayMessages = delayMessages   ( Delay ? 1 : -1 );

The result of the expression with the conditional operator is -1 because the first sub-expression (Delay) evaluates to false.

So in fact you have

delayMessages = delayMessages   -1;

The variable delayMessage declared like

bool delayMessages=0;

has the value false according to the quotes from the section 7.14.

In the expression with binary plus operator it is converted to the integer 0 according to the quotes (7.6 Integral promotions) and 8.7 Additive operators and you have

delayMessages = 0   -1;

or

delayMessages = -1;

Again according to the quote 7.14 Boolean conversions the result value of the variable delayMessage will be true.

The operator << outputs a boolean value true as 1 in this statement

std::cout << "Hello world!"<<delayMessages;

CodePudding user response:

The expression on the right hand side is evaluated like an if-statement.

if (Delay == true)
   return 1;
else
   return -1;

The result is then used for the = assignment.

In the C 20 draft standard that's

7.6.19 (6) (Assignment and compound assignment operators)

The behavior of an expression of the form E1 op= E2 is equivalent to E1 = E1 op E2 except that E1 is evaluated only once. [...]

Since Delay == false, the return value of the ternary operator is -1. The fact that you're operating on a boolean instead of an int can make it look like you got the 1 back.

Note that you get a compiler warning C4804:

warning C4804: ' =': unsafe use of type 'bool' in operation

Is it undefined behavior? No.

7.6.19 (6) (Assignment and compound assignment operators)

[...] For = and -=, E1 shall either have arithmetic type or be a pointer to a possibly cv-qualified completely-defined object type. In all other cases, E1 shall have arithmetic type.

and

7.3.8 (2) (Integral conversions)

If the destination type is bool, see 7.3.14.

which says

7.3.14 (1) (Boolean conversions)

A prvalue of arithmetic, unscoped enumeration, pointer, or pointer-to-member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.

So the -1 is converted to true and true prints as 1.

CodePudding user response:

 Delay ? 1 : -1

evaluates to -1 because Delay is false. Converting -1 to bool yields true because only 0 is converted to false. Then printing the true prints 1. The result is true no matter if Delay is true or false and 1 is printed in both cases.

Use true / false for booleans and use an integer type for integers. Perhaps this is what you actually wanted to do:

int delayMessages=0;
bool Delay = false;
delayMessages  = Delay ? 1 : -1;
std::cout << "Hello world!"<<delayMessages;

CodePudding user response:

delayMessages  = Delay ? 1 : -1;

This basically means if Delay is true, then delayMessages = 1 (which is 1), else delayMessages = -1 (which is also 1).

  • Related