Search code examples
clogical-operatorslogical-or

Explain the logic


  #include <stdio.h>
 //Compiler version gcc 6.3.0

 int main(void)
 {
    int i=10, j=2, k=0, m;
    m=++i || ++j && ++k;
    printf("%d,%d,%d,%d",i,j,k,m);
 }

can anyone explain the logic, Output is 11,2,0,1


Solution

  • i starts as 10, but is incremented to 11, before it is tested.

    m is being assigned a boolean result, so will be either 0 or 1.

    i is non zero so evaluates to true for a boolean expression, therefore, the expression after the || do not need to be evaluated, as || is a boolean short circuit evaluator.

    Hence your output.