C Language Learning Notes: 100 Articles – 23. Operator Precedence and Associativity: Just Remember This One Mnemonic, Which Is… Add Parentheses

“From today on, study hard and make progress every day”

Repetition is the best method for memory; spend one minute every day to remember the basics of C language.

C Language Beginner’s Essential Knowledge Notes Series of 100 Articles

23. Operator Precedence and Associativity: Just Remember This One Mnemonic, Which Is Add Parentheses

1. Basic Concept of Operator Precedence

Operator precedence determines the order of evaluation of different parts of an expression. When an expression contains multiple operators, the operator with higher precedence is evaluated first:

int result = 5 + 3 * 2;  // First calculate 3*2, then +5 → 11

2. C Language Operator Precedence Table (from High to Low)

For exams, you need to memorize this; in actual programming, if you don’t remember, just add parentheses!!!

| Precedence | Operator Category | Operator Examples | Associativity || —— | ———- | ————————- | —— | —– | —– || 1 | Postfix Operators | () [] -> . ++ — | Left→Right || 2 | Unary Operators | + – ! ~ ++ — * & sizeof | Right→Left || 3 | Multiplication/Division/Modulus | * / % | Left→Right || 4 | Addition/Subtraction | + – | Left→Right || 5 | Shift | << >> | Left→Right || 6 | Relational Operators | < <= > >= | Left→Right || 7 | Equality Operators | == != | Left→Right || 8 | Bitwise AND | & | Left→Right || 9 | Bitwise XOR | ^ | Left→Right || 10 | Bitwise OR | | | Left→Right || 11 | Logical AND | && | Left→Right || 12 | Logical OR | || | Left→Right || 13 | Conditional Operator | ?: | Right→Left || 14 | Assignment Operators | = += -= *= /= %= &= ^= | = | Right→Left || 15 | Comma Operator | , | Left→Right |

3. Detailed Explanation of Associativity

When operators have the same precedence, associativity determines the order of evaluation:

1. Left Associative (from Left to Right)

int a = 10 - 5 - 2;  // First calculate 10-5, then -2 → 3

2. Right Associative (from Right to Left)

int b = x = y = 5;   // First y=5, then x=y, finally b=x

4. Common Precedence Issues

1. Pointer Dereference vs Increment

int arr[] = {1,2,3};
int *p = arr;
int a = *p++;    // Equivalent to *(p++)
int b = (*p)++;  // First get *p, then increment the value of (*p)

2. Logical Operator Precedence

int x = 1, y = 0;
if (x > 0 && y++ > 0)  // && has lower precedence than >, equivalent to (x>0) && (y++>0)

3. Bitwise Operations vs Comparison Operations

int flags = 0x05;
if (flags & 0x01 == 1)  // Error: == has higher precedence than &
                        // Equivalent to flags & (0x01 == 1)

5. Memory Techniques

  1. 1. Mnemonic Memory Method:
     Single Unary Arithmetic Relational Logical Conditional Assignment Comma
    (Unary→Arithmetic→Relational→Logical→Conditional→Assignment→Comma)
  2. 2. Special Operator Memory:
  • <span>-></span><span>.</span><span>[]</span><span>()</span> have the highest precedence
  • <span>!</span><span>~</span><span>++</span><span>--</span><span>*</span><span>&</span> and other unary operators come next
  • <span>?:</span> and <span>=</span>, comma and other operators have the lowest precedence

6. Usage Techniques

  1. 1. Use Parentheses Wisely:
    // Do not rely on precedence memory
    int result = (a + b) * (c - d);
  2. 2. Split Complex Expressions:
    // Not recommended
    x = a + b * c >> d & e | f;
    // Recommended
    temp = a + b * c;
    x = (temp >> d) & e | f;
  3. 3. Avoid Ambiguous Writing:
    // Not recommended
    if (x & 0xFF == 0x80)
    // Recommended
    if ((x & 0xFF) == 0x80)

Some students contacted me, wanting to have a study exchange group. Previously, I was worried about advertisements, so I didn’t create a group. Considering that having a group is indeed convenient, I will create one this time to try it out.

If you need it, hurry up and join; the validity period is 7 days.

C Language Learning Notes: 100 Articles - 23. Operator Precedence and Associativity: Just Remember This One Mnemonic, Which Is... Add Parentheses

———- End ———-

[Special Statement: All articles in this public account are original or authorized by the author, please feel free to consume them; the views are for learning reference only~~]

C Language Learning Notes: 100 Articles - 23. Operator Precedence and Associativity: Just Remember This One Mnemonic, Which Is... Add Parentheses

C Language Learning Notes: 100 Articles - 23. Operator Precedence and Associativity: Just Remember This One Mnemonic, Which Is... Add Parentheses

“If you like C, please like it”C Language Learning Notes: 100 Articles - 23. Operator Precedence and Associativity: Just Remember This One Mnemonic, Which Is... Add Parentheses Click the bottom right corner to seeC Language Learning Notes: 100 Articles - 23. Operator Precedence and Associativity: Just Remember This One Mnemonic, Which Is... Add Parentheses

Leave a Comment