Back to C Programming
2026-01-145 min read

Operator precedence

Learn Operator precedence step by step with clear examples and exercises.

Why This Matters

Understanding operator precedence in C programming is crucial for writing efficient and bug-free code. Operator precedence determines how expressions are evaluated, affecting the final output of your program. Mastering this concept can help you avoid common mistakes, debug real-world issues, and ace coding interviews.

When multiple operators are used together in an expression, operator precedence dictates the order in which they are processed. This can significantly impact the result of a calculation, making it essential to understand how operator precedence works in C programming.

Prerequisites

Before diving into operator precedence, it's important to have a solid understanding of the following concepts:

  1. Basic C syntax and data types (variables, constants, operators)
  2. Control structures (if-else, loops)
  3. Functions and function calls
  4. Arrays and pointers
  5. Understanding of basic mathematical operations
  6. Familiarity with logical operators (&&, ||, !)
  7. Knowledge of C data types such as int, float, char, and bool
  8. Basic understanding of bitwise operators (&, |, ^, ~, <<, >>)

Core Concept

Operator precedence in C programming determines the order in which operators are evaluated within an expression. Operators are categorized into several groups based on their precedence, with higher-precedence operators being evaluated before lower-precedence ones. Here's a summary of the most commonly used operators and their associativity:

  1. Suffix/postfix increment and decrement (++, --)
  • Precedence: Highest
  • Associativity: Right-to-left
  • Example: i++ increments the value of i by 1 after the expression is evaluated.
  1. Function call, comma, conditional operator ((), , ?:)
  • Precedence: High
  • Associativity: Left-to-right
  • Example: func(), expr1, expr2
  1. Multiplication, division, and remainder (*, /, %)
  • Precedence: Medium-high
  • Associativity: Left-to-right
  • Example: 5 * 3 / 2 is first multiplied, then divided.
  1. Addition and subtraction (+, -)
  • Precedence: Medium
  • Associativity: Left-to-right
  • Example: 5 + 3 - 2 is first added, then subtracted.
  1. Bitwise left shift and right shift (<<, >>)
  • Precedence: Low-medium
  • Associativity: Left-to-right
  • Example: 1 << 2 shifts the binary representation of 1 two places to the left.
  1. Relational operators (<, >, <=, >=, ==, !=)
  • Precedence: Low
  • Associativity: Left-to-right
  • Example: 5 < 3 && 7 > 8 is first compared for the less-than operator, then for the greater-than operator.
  1. Bitwise AND (&), Bitwise XOR (^), and Bitwise OR (|)
  • Precedence: Lowest
  • Associativity: Left-to-right
  • Example: 5 & 3 performs a bitwise AND operation on the binary representations of 5 and 3.

Operator Precedence Examples

Let's look at some examples to better understand operator precedence in C programming:

int a = 10, b = 5, c;

// Example 1: Multiplication before addition
c = a * b + 2; // c will be assigned the value of (10 * 5) + 2 = 62

// Example 2: Bitwise AND before bitwise OR
c = ~a & b | ~b; // c will be assigned the value of ~10 & 5 | ~5 = 31

Operator Precedence and Parentheses

Parentheses can be used to explicitly define the order in which operators are evaluated. For example:

int x = (3 + 4) * 2 - 1; // The parentheses ensure that addition is performed before multiplication, resulting in x being assigned the value of (7) * 2 - 1 = 10

Worked Example

In this section, we'll walk through a worked example to help you understand operator precedence better. Let's take the following expression:

int x = 3 + 4 * 2 - 1;

First, we follow the order of operators from highest to lowest precedence:

  1. Multiplication: 4 * 2 equals 8.
  2. Addition: 3 + 8 equals 11.
  3. Subtraction: 11 - 1 equals 10.

So, the final value of x will be 10.

Common Mistakes

  1. Forgetting parentheses: Failing to use parentheses can lead to incorrect results due to operator precedence. For example:
int a = 3 + 4 * 2; // a will be assigned the value of (3 + 4) * 2 = 18, not 14

Solution: Use parentheses to explicitly define the order of operations.

  1. Misunderstanding associativity: Some operators are left-associative, while others are right-associative. Failing to understand this can lead to incorrect results. For example:
int a = 3 + 4 + 5; // a will be assigned the value of (3 + 4) + 5 = 12, not 18

Solution: Use parentheses to explicitly define the grouping of operations.

  1. Misusing prefix and suffix increment/decrement operators: Prefix increment/decrement operators are evaluated before suffix ones, so mixing them can lead to unexpected results. For example:
int a = 5;
int b = ++a + a++; // b will be assigned the value of (++a) + a = 7 + 6 = 13, not 14

Solution: Use either prefix or suffix operators in a single expression.

  1. Ignoring type promotion rules: Some operations may require type promotion, which can affect the final result. For example:
int a = 5;
char b = '3';
int sum = a + b; // sum will be assigned the value of (5 + '3') = 51, not 8

Solution: Be aware of type promotion rules and use casts when necessary.

Practice Questions

  1. Calculate the result of 7 * 3 / 2 - 1.
  2. Given int a = 5 & 3 | 4, what is the value of a?
  3. Write an expression that calculates the average of three numbers (num1, num2, and num3) using only parentheses and basic arithmetic operators.

FAQ

Q: What happens if I mix prefix and suffix increment/decrement operators in the same expression?

A: Prefix increment/decrement operators are evaluated before suffix ones, so mixing them can lead to unexpected results. For example:

int a = 5;
int b = ++a + a++; // b will be assigned the value of (++a) + a = 7 + 6 = 13, not 14

Solution: Use either prefix or suffix operators in a single expression.

Q: How can I force the evaluation order of operators to change?

A: You can use parentheses and group operations explicitly to change the evaluation order. For example:

int x = (3 + 4) * 2 - 1; // The parentheses ensure that addition is performed before multiplication, resulting in x being assigned the value of (7) * 2 - 1 = 10

Q: What are some common type promotion rules in C?

A: In C, when performing arithmetic operations involving operands of different types, the operands are promoted to a higher data type. Here are some common type promotion rules:

  • If either operand is a float, both operands are promoted to double.
  • If both operands are integers, they are promoted to int or unsigned int, whichever is large enough to hold the values of both operands.
  • If one operand is a character and the other is an integer, the character is converted to an integer (ASCII value).

Q: What are some common pitfalls when using bitwise operators?

A: Some common pitfalls when using bitwise operators include:

  • Misinterpreting the results of bitwise operations due to a lack of understanding of binary representations.
  • Failing to account for sign extension when performing arithmetic with signed integers and bitwise AND (&) or left shift (<<) operations.
  • Using bitwise operators inappropriately, such as using | instead of || for logical OR or using ^ instead of != for logical XOR.