1

Summary

1) The && expression calculated from left to right. When the condition is false, the entire expression is false, and the following expressions are not executed anymore, which is the short-circuit rule;

2) ||The expression calculated from left to right. When the condition is true, the entire expression is true, and the following expressions will not be executed anymore, which is the short-circuit rule;

3) In logical expressions && has a higher priority than ||, which is expressed as: When && and || appear in the same expression, the entire expression is regarded as a || expression. First calculate the && expression, and finally calculate the entire || expression. When an expression is true, the entire || expression is short-circuited.

4) The logical not "!" C language only recognizes 0 (false), and any other value is not 0 (true).
"!" is often used in if statement to determine whether an expression is false; it can also be used to change an integer to 1, such as !!100;

Hidden knowledge points of logical operators

1. The short-circuit rule in && and ||

  • || from left to right: stop the calculation when the condition is true, the whole expression is true; the expression is false only when all the conditions are false

    int LogicFunc(int i)
    {
      printf("LogicFunc(int i) : %d\n", i);
      return i;
    }
    
    int main()
    {
    
      if(LogicFunc(1) || LogicFunc(0))
      {
          printf("Logic expression is true");
      }
      else
      {
          printf("Logic expression is false");
    
      }
    }
    输出:
    LogicFunc(int i) : 1
    Logic expression is true
    
    原因:逻辑||表达式的短路规则,第1个表达式LogicFunc(1)返回1,为true,第二个表达式由于短路规则并未执行
  • && Start calculation from left to right: stop calculation when the condition is false, the entire expression is false; the expression is true only when all the conditions are true

    int LogicFunc(int i)
    {
      printf("LogicFunc(int i) : %d\n", i);
      return i;
    }
    
    int main()
    {
    
      if(LogicFunc(0) && LogicFunc(1))
      {
          printf("Logic expression is true");
      }
      else
      {
          printf("Logic expression is false");
    
      }
    }
    输出:
    LogicFunc(int i) : 0
    Logic expression is false
    
    原因:逻辑&&表达式的短路规则,第1个表达式LogicFunc(0)返回0,false,第二个表达式由于短路规则并未执行

2. Rules for && and || mixed operations

In logical expressions && has a higher priority than ||, which is expressed as: When && and || appear in the same expression, the entire expression is regarded as a || expression. Calculate && first, then || last.

  • Code reading

    // 以下代码的输出是?
      int i = 0;
      int j = 0;
      int k = 0;
    
      int t = ++i || ++j && ++k;
    
      printf("t = %d\n", t);
      printf("i = %d\n", i);
      printf("j = %d\n", j);
      printf("k = %d\n", k);
  • Code analysis

    输出:
    t = 1
    i = 1
    j = 0
    k = 0
    
    分析:
    ++i || ++j && ++k;  ==> 等价于一个逻辑或表达式 (1 && ++i) || (++j && ++k);
    左边的逻辑与表示,1为真,继续计算++i,i自增1,然后取值,所以左侧表达式也为真
    这时候遇到了逻辑或表达式的短路规则:左侧表达式已经为真,右侧的表达式不再计算。
    因此,j和k仍然为0

3. "!" What is the magic horse

The logical negation operator in C language only recognizes 0, and returns 1 when it encounters 0; otherwise, it returns 0;

    printf("%d\n", !0);        // 1
    printf("%d\n", !-1);       // 0
    printf("%d\n", !100);      // 0
    printf("%d\n", !!100);     // 1

This article is summarized from "C Language Advanced Course" by Tang Zuolin from "Ditai Software Academy".
If there are any errors or omissions, please correct me.


bryson
169 声望12 粉丝