โž• C Operators
Estimated reading: 3 minutes 6 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿงฉ C Operator Precedence

Or Copy Link

CONTENTS
Scroll to Top