What is the point of the logical operators in C?

后端 未结 5 1999
被撕碎了的回忆
被撕碎了的回忆 2020-12-18 07:49

I was just wondering if there is an XOR logical operator in C (something like && for AND but for XOR). I know I can split an XOR into ANDs, NOTs and ORs but a simple

5条回答
  •  死守一世寂寞
    2020-12-18 08:19

    The bitwise operators do not work "just the same" as the && and || operator. For a start, && and || perform short-circuited evaluation, whereas the the bitwise operators do not. In other words, you can't do something like this with the bitwise operators:

    int * p = 0;
    (p != 0) && (*p = 1);
    

    because if you said:

    (p != 0) & (*p = 1);
    

    both subexpressions would be evaluated, and you would dereference the null pointer.

提交回复
热议问题