# C language? Don’t you understand logical operators? An article teaches you to distinguish between “true and false”!

Time：2021-9-16

I have met some friends in the past two days. I don’t know the logical operation symbols and what they mean. Let’s talk about logical operators briefly today. ### Logical operator

Relational operators only test the relationship between the left and right values (compare them with each other), while logical operators combine multiple relational expressions to judge whether the final result is “true” or “false”. Therefore, sometimes logical operators are also called compound relation operators.

In high school mathematics, we have learned logical operations. For example, P is a true proposition and Q is a false proposition. Then “P and Q” is false, “P or Q” is true and “non-Q” is true.

In C language, there are similar logical operations: For the priority of logical operators, logical non! Is the highest priority, not only prior to relational operators, but also prior to arithmetic operators, followed by logic and & &, logic or | has the lowest priority, while logic and & & and logic or | have lower priority than relational operators. When relational operators, logical operators and arithmetic operators appear in a judgment condition expression at the same time, the operation priority relationship is as follows:

() → function! → *、/、%→+、-→、!=、<=、>= →&&、||

### Result of logical operation

In programming, we generally call zero value “false” and non-zero value “true”. The result of logical operation is only “true” and “false”. The value corresponding to “true” is 1 and the value corresponding to “false” is 0.

#### 1) Sum operation (& &)

The result is true only when both quantities involved in the operation are true, otherwise it is false. For example:

5&&0

5 is true, 0 is false, and the result of phase and is false, that is, 0.

(5>0) && (4>2)

5> The result of 0 is 1, which is true, and the result of 4 > 2 is 1, which is also true, so the result of sum is true, which is 1.

#### 2) Or operation (|)

As long as one of the two quantities involved in the operation is true, the result is true; When both quantities are false, the result is false. For example:

10 || 0

10 is true, 0 is false, and the result of phase or is true, that is, 1.

(5>0) || (5>8)

5> The result of 0 is 1, which is true, and the result of 5 > 8 is 0, which is false, so the result of phase or is true, that is, 1.

#### 3) Non operation (!)

When the quantity involved in the operation is true, the result is false; When the quantity involved in the operation is false, the result is true. For example:

!0

0 is false, and the result of non operation is true, that is, 1.

!(5>0)

5> The result of 0 is 1, which is true, and the result of non operation is false, that is, 0.

Output the result of logical operation:

#include

int main(){

int a = 0, b = 10, c = -6;

int result_1 = a&&b, result_2 = c||0;

printf(“%d, %d\n”, result_1, !c);

printf(“%d, %d\n”, 9&&0, result_2);

printf(“%d, %d\n”, b||100, 0&&0);

return 0;

}

0, 0

0, 1

1, 0

#### priority

The priority of logical operators and other operators from low to high is:

Assignment operators (=) < & & and | < relational operators < arithmetic operators < non (!)

&&And | are lower than relational operators,! Higher than arithmetic operator.

According to the priority of operators, we can get:

a>b && c>d   Equivalent to   (a>b)&&(c>d)

!b==c||d

a+b>c&&x+yc)&&((x+y)

In addition, logical expressions can also be nested, such as a > b & & B | 9 > C, a | C > D&&   ! p。

Examples of logical operators:

#include

int main(){

char c=’k’;

int i=1,j=2,k=3;

float x=3e+5,y=0.85;

printf( “%d,%d\n”, !x*!y, !!!x );

printf( “%d,%d\n”, x||i&&j-3, i

printf( “%d,%d\n”, i==5&&c&&(j=8), x+y||i+j+k );

return 0;

}

Operation results:

0,0

1,0

0,1

In this case! X and! Y is 0,! x*! Y is also 0, so its output value is 0. Because x is not 0, so!!! The logical value of X is 0. For the formula x | I & & J-3, first calculate that the value of J-3 is non-0, and then find that the logical value of I & & J-3 is 1, so the logical value of X | I & & J-3 is 1. Right I

That’s all for today’s knowledge sharing about C language logical operators. I hope it can help you. If you want to better improve your programming ability, learn C language and C + + programming well! Overtake at the corner, take a quick step!
C language c + + Learning Circle】, sharing (source code, project actual combat video, project notes, basic introductory tutorial)
Welcome to change careers and learn programming partners. Use more materials to learn and grow faster than you think!

Programming learning books: Programming learning video: ## Example of method for converting Lua table to c# dictionary

Table property Table is an “associative array”. The index of the array can be a number or a string. All index values need to be enclosed by “[” and “]”; If it is a string, you can also remove quotation marks and brackets; That is, if it is not enclosed by [], it is considered […]