Why are `(a -> 0)` and `((a -)> 0)` the same?

The program works like

main() { int a=1; if( a-- > 0) printf("AAAA"); else printf("BBBB"); } 

Its output is AAAA and if I use

 main() { int a=1; if( (a--) > 0) printf("AAAA"); else printf("BBBB"); } 

then why the AAAA output again. () has more preferences than -- .

+4
source share
5 answers

Postfix operator -- has a higher priority than any boolean comparison operator.

What do you expect? a-- always evaluates the value of a , which decreases after the estimate.

+9
source

The postfix operator -- returns the original value of a variable, even after decreasing it.

So yes, a decreased to comparison, but the result of the expression a-- not a , but the value 1 .

+2
source

-- reduces the value of the variable after its use in the expression.

0
source

The use of parentheses here does not affect the code, because the evaluation order is the same with and without parentheses. You are correct that parentheses take precedence over -. However, in this case, the brackets will not change the order of evaluation, because you did not group the operands in a different order than they could naturally evaluate.

0
source

Here is a link with all operator precedence in C ++.

0
source

Source: https://habr.com/ru/post/1338223/


All Articles