Short circuit behavior of logical expressions in C in this example

前端 未结 1 833
抹茶落季
抹茶落季 2020-11-27 19:38

PROGRAM

#include 

int main(void)
{
    int i, j, k;

    i = 1; j = 1; k = 1;

    printf(\"%d \", ++i || ++j && ++k);
    printf(\"%         


        
相关标签:
1条回答
  • 2020-11-27 19:43

    Precedence affects only the grouping. && has a higher precedence than || means:

    ++i || ++j && ++k
    

    is equivalent to:

    ++i || (++j && ++k)
    

    But that doesn't mean ++j && ++k is evaluated first. It's still evaluated left to right, and according to the short circuit rule of ||, ++i is true, so ++j && ++k is never evaluated.

    0 讨论(0)
提交回复
热议问题