Logical operators and their priority, C language, and priority logical operators Explanation

C language provides three logical operators.

  1.  One yuan:! (Logical negation).
  2.  Two yuan: && (logical AND), || (logical OR).


These three logical operators, the logic NOT! Highest priority, followed by a logical AND &&, ||, or the lowest priority logic. I.e. priority order of arithmetic, logic, as the assignment operator:

Logic NOT!> Arithmetic> && logical and, logical or ||> = Assignment

Value is a logical value of the logical expression, namely Boolean (bool), the new C99 type, some compilers may not support this type.

Logical value into logical true and logical false value. In general, when it is determined, only the value zero is determined as a logical false value (to false), all non-zero value can be determined as a logical true value (true); and when storing said general, represents logic 1 true value, 0 represents a logical fALSE value.

And logical operators && operation rules: only two operands are logical true, the result will be true. In other cases, the results were false.

Logic or arithmetic operators || rules: only two operands are logically false, only the result is false. The remaining cases, the results are true.

For example, with a definition statement

  1. int a=3,b=5;

There are:

  • ! A:! Since a non-zero, is true, a false, a value of 0.
  • a || b: zero since a and b, are true, it evaluates to true or a logic value 1.
  • a && b: zero since a and b, are true, so the logical AND result is true, a value of 1.
  • ! A || b && 2: due to a logical non-highest priority, is first combined with a, && and || higher priority than, equivalent || (b && 2), i.e., 0 || 1 is true, the value! (A!) 1.


Logical AND &&, || are logical or "short circuit" feature:

  • And && Logical "short circuit": when the left operand in the logical && is a logical false, it is sufficient to determine whether the logic operation is false, and it is no longer right operand is performed.
  • || logical or "short circuit": When the left operand logic or logically true when ||, is sufficient to determine whether the logical operation is true, and it is no longer right operand is performed.


E.g:

  1. int a=1,b=2,c;
  2. c=a||++b;
  3. printf("a=%d,b=%d,c=%d\n",a,b,c);

Since a non-zero value, that is true, and when the left operand is logically true or ||, enough to predict the result of the logical operation is true. Therefore, the occurrence of "short", i.e., the right operand ++ b is not performed. The output is: a = 1, b = 2 , c = 1.

The following analysis program, the output results of its operation.

  1. #include<stdio.h>
  2. int main(void)
  3. {
  4. int a=0,b=2,c;
  5. c=!a||++b&&a--;
  6. printf("a=%d,b=%d,c=%d\n",a,b,c);
  7. return 0;
  8. }

Code Analysis:
mixing operator expression c = a || ++ b && a-- logic contained in the non-!! , Logic, or ||, && logic and arithmetic ++ prefix, suffix arithmetic -, an assignment operator = six. Priority relationship logical operators, arithmetic operators, assignment operators are:

Logic NOT!> Arithmetic> && logical and, logical or ||> = Assignment

Since the expression assignment operator has the lowest priority, so the final assignment.

The level of priority, the expression! A || ++ b && a-- is equivalent to (! A) || ((++ b) && (a--)), and the left operand of a logical or ||! a is true, then sufficient to determine whether the expression is true. Therefore, the occurrence of "short", i.e., the entire operation of the right number of || ((++ b) && (a-- )) is no longer performed.

Run results:
A = 0, B = 2, C =. 1

Guess you like

Origin www.cnblogs.com/HGNET/p/11791471.html