โž• C Operators
Estimated reading: 3 minutes 191 views

C Operator Precedence โ€“ Complete Table with Examples

Introduction โ€“ What Is Operator Precedence in C?

Operator precedence in C defines the order in which operators are evaluated in complex expressions. When multiple operators appear in a single expression, precedence determines which operations are performed firstโ€”just like the order of operations in mathematics (e.g., multiplication before addition).

In this guide, youโ€™ll learn:

  • What operator precedence and associativity mean
  • Complete table of C operator precedence
  • Examples where precedence affects result
  • Best practices using parentheses for clarity

What Is Precedence and Associativity?

  • Precedence determines which operator is evaluated first.
  • Associativity determines the direction of evaluation (left-to-right or right-to-left) when operators have the same precedence.

Example:

int result = 5 + 2 * 3;  // result = 11 (not 21)

Here, * has higher precedence than +.


C Operator Precedence Table (Highest to Lowest)

PrecedenceOperatorsAssociativityDescription
1() [] -> .Left to RightFunction call, array, struct access
2++ -- + - ! ~ * & sizeofRight to LeftUnary, address-of, dereference
3* / %Left to RightMultiplication, division, modulus
4+ -Left to RightAddition, subtraction
5<< >>Left to RightBitwise shift left, right
6< <= > >=Left to RightRelational operators
7== !=Left to RightEquality/inequality
8&Left to RightBitwise AND
9^Left to RightBitwise XOR
10``Left to Right
11&&Left to RightLogical AND
12``
13?:Right to LeftTernary conditional
14= += -= *= /= etc.Right to LeftAssignment
15,Left to RightComma (sequence)

Examples of Operator Precedence

Without Parentheses

int a = 10, b = 5, c = 2;
int result = a + b * c;  // b * c = 10, then 10 + 10 = 20

With Parentheses

int result = (a + b) * c;  // a + b = 15, then 15 * 2 = 30

Associativity Example

int a = 5;
int result = a = a + 2 * 3;  // 2 * 3 = 6, then a + 6 = 11, then a = 11

In a = a + 2 * 3;, the * is evaluated first, then +, then =.


Best Practices

  • Always use parentheses to clarify the intended order.
  • Avoid writing complex one-liners with multiple operators.
  • Use indentation and line breaks for readability when needed.
  • Donโ€™t assume operator precedenceโ€”look it up or add parentheses.

Summary โ€“ Recap & Next Steps

Operator precedence and associativity in C are vital for writing correct and predictable expressions. Misunderstanding precedence can lead to logical errors and unexpected results.

Key Takeaways:

  • Precedence defines which operators are evaluated first.
  • Associativity defines direction (left-to-right or right-to-left).
  • Use parentheses to control or override the default precedence.
  • Multiplication and division have higher precedence than addition/subtraction.

Real-World Relevance:

Operator precedence is crucial in expression-heavy code, such as mathematical computations, conditional logic, algorithm design, and embedded C programming where performance and correctness matter.


Frequently Asked Questions (FAQ)

What is operator precedence?

It determines the priority of operators in expressions without parentheses. Operators with higher precedence are evaluated before lower ones.


What is operator associativity?

When multiple operators with the same precedence appear, associativity defines the evaluation directionโ€”usually left-to-right.


How do I ensure correct evaluation?

Use parentheses () to explicitly define the order of operations:

int result = (a + b) * c;

Does && have higher precedence than ||?

Yes. Logical AND (&&) has higher precedence than Logical OR (||).


Can I rely on operator precedence across compilers?

Yes. C operator precedence is defined by the language standard and behaves the same across standard-compliant compilers.


Share Now :
Share

๐Ÿงฉ C Operator Precedence

Or Copy Link

CONTENTS
Scroll to Top