Introduction: Many people believe that priority represents the order of operations, influenced by mathematics or everyday life, making them sensitive to this term. Isn’t priority just about who gets processed first? However, in C language, it is not quite the case.
Priority only determines the order of association in expressions, not the order of operations!
Don’t argue just yet; it’s more useful to rely on facts than to argue. This is how we can see who is a novice and who is an expert.
Today, we will discuss three logical operators: logical AND (binary), logical OR (binary), and logical NOT (unary).
If this article changes your understanding, please consider bookmarking and sharing it.
Main Content:
1. I’ll put the priority table here first.

According to this table, we can easily conclude the priority:!>&&&>||
We can reach a consensus here; next, let’s look at a counter-example.
2. Now let’s look at a piece of code to verify this conclusion.
#include<stdio.h>int main() { int x = 3; int y = 1 || (x = 1) && (x += 1); printf("x = %d , y = %d", x, y); return 0;}
What do you think the result of this code will be?
Let’s go through it:
First, int x=3; then execute the next statement. Since && has a higher priority than ||, (x=1) && (x+=1) should be executed first, so logically, x should be 2 and y should be 1.
The execution result is as follows:

But in fact: Output: x = 3, y = 1
Why?? What went wrong?
If the order was wrong, how could x=3?
If you understand the short-circuit effect of || and && (the former does not execute the latter if the first is true, and the latter does not execute the former if the first is false), you might know that to make x=3, it seems that the two statements need to short-circuit, meaning the first part of || should execute first. Isn’t && supposed to have a higher priority than ||? How could this happen? Is there a bug in the code? A problem with the compiler? A virus on the computer? Or is the priority table wrong?
What is the reason? Let me analyze it for you slowly.
In Baidu’s literature on operator priority, there is a statement:

The order of association for expressions depends on the priority of various operators in the expression!
Clearly, this is the role of priority. Returning to the code at the beginning, you will find that I have always judged the calculation order based on priority, assuming that the higher priority executes first and the lower priority executes later, which led to an incorrect judgment. So why does it output x=3?
If we consider the association, it essentially adds a layer of parentheses. Thus, according to the order of association, we get the following code:
int y = 1 || ((x = 1) && (x += 1));
So && has a higher priority than ||, only determining this layer of parentheses, while the order of operations still proceeds from left to right, hence the statements after || short-circuit.
Conclusion: Priority only determines the order of association in expressions, not the order of operations!
**********I am the dividing line**********
Someone is bound to ask: Why did I get it right before with that kind of thinking?
That was purely coincidental; the problems were too simple, and when faced with more difficult ones, the flaws will be exposed.
Another example:
Recently, while doing exercises, I had some doubts about operator priority?

Common understanding:
= has a higher priority than !=, so the expression is equivalent to a != (3 >= 1), first executing 3 >= 1 which is true, returning 1, then executing a != 1 which is false, returning 0, the result does not print.
However, the correct way of thinking is like this:
if(a != 3 >= 1)
This expression, according to the logical operator priority, is equivalent to:
if(a!=(3>=1))
The operation process is: from left to right, first step a != expression, second step calculate the value of the expression, third step return the result.
Because (3>=1) is part of the first logical condition (a!=xxx) (in fact, there is only one logical condition), so the determination of the first logical expression from left to right requires the calculation of (3>=1).
i.e., a!=1, 1!=1, so the value in the if parentheses is 0, the condition is false, and the loop is not executed.
The main difference between these two ways of thinking is theorder of execution. Although both lead to the same result (which is also the case with the simple problems usually solved), the first way is fundamentally wrong (don’t think I’m exaggerating).
Do not take this lightly, thinking this is a minor issue; it is a serious problem.
Do not take this lightly, thinking this is a minor issue; it is a serious problem.
Do not take this lightly, thinking this is a minor issue; it is a serious problem.
The true role of priority is only to determine which operators and which sub-expressions are associated.
**********I am the dividing line**********
Here is another question screenshot (click the image to enlarge)

Now you understand, right? In learning, do not fear in-depth study; what matters is being meticulous and serious.
The first half of the article is sourced from
———————
Author: Italink
Source: CSDN
Original: https://blog.csdn.net/qq_40946921/article/details/88323975
Copyright Statement: This article is an original article by the blogger, please attach the blog link when reprinting!
The second half of the article is sourced from
Original: https://q.cnblogs.com/q/113431/

If you have any questions or errors regarding the article, feel free to discuss them with me.
If you think the article is good, please consider rewarding and sharing before you leave.
Your support is greatly appreciated.