Explanation for this function's output
c, integer-arithmetic, logical-operators, output
Solution
The key concept to understanding the result is short-circuit evaluation of Boolean operators (`&&` and `||`) -- if, after evaluating the left-hand side of a Boolean operator, the value of the right-hand side cannot affect the overall result, then it will not be evaluated and any side-effects it would produce will not happen.
In the first case, since `--a` evaluates to `0` (=false) the second part of `... && ...` is not evaluated, since "false AND anything" will always be false. Specifically, `b++` is never executed, and so its value remains `1` in the output.
In the case of `--a || b++`, the value of the whole expression cannot be determined by the left-hand side ("false OR something" can still be true) so the `b++` is evaluated (and it's side-effect, incrementing `b`, happens).
The other concept needed to fully understand the results is the difference between pre- and post-increment/decrement operators. If the `--` or `++` appears before the variable (as in `--a`) then the variable is decremented or incremented first and new value is used to evaluate the whole expression. If the `--` or `++` appears after the variable (as in `b++`) then the current value of the variable is used to evaluate the expression and the increment/decrement happens after this has happened.
It should be noted that expressions that try to combine two or more instances of `--`/`++` of the same variable (e.g. `a++ + ++a`) are quite likely to invoke undefined behaviour -- the result may vary by platform, compiler, compiler and even the time of day.
Problem
I am doing review questions which ask me "What is the output of the following," and I am having some trouble understanding something about this function: ``` int a = 1, b = 1, c = -1; c = --a && b++; printf("%d %d %d", a, b, c); ``` The output is 010. My question is about line 2, `c = --a && b++`. How is this line processed, and how does it work/change the values? And if it were `c = --a || b++`? From my understanding I thought the output would be 020.