0

Can somone please tell me why does it print 1 2 1 1 instead of printing 1 2 2 2

#include <stdio.h>
    #include <stdlib.h>

    int main()
    {

        int i = 1, j = 1, k = 1;
        printf("%d ", ++i || ++j && ++k);
        printf("%d %d %d", i, j, k);
        return 0;
    }
Michael
  • 82
  • 5
  • 2
    Three things to consider: 1) Any non-zero value is considered `true` 2) `||` and `&&` are short-circuiting operations 3) Mind the operator precedence of `&&` and `||`. – Eugene Sh. Nov 22 '19 at 17:55
  • If `i != 0` then neither `j` nor `k` will be incremented. Watch out for side-effects in condition expressions not happening when the expression is not evaluated. – Weather Vane Nov 22 '19 at 17:56

0 Answers0