0Pricing
C Academy · Lesson

Operator Precedence

Evaluation order.

Operator Precedence is a free C Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Precedence Matters

Precedence decides which operator runs first in an expression with several operators. It mirrors normal math: multiplication before addition.

#include <stdio.h>
int main(void) {
    printf("%d\n", 2 + 3 * 4);
    return 0;
}

Parentheses Override

Parentheses force evaluation order. Use them to make intent clear and override defaults.

#include <stdio.h>
int main(void) {
    printf("%d\n", (2 + 3) * 4);
    return 0;
}

Arithmetic Precedence

Among arithmetic operators: * / % bind tighter than + -. The high-precedence ones run first.

#include <stdio.h>
int main(void) {
    int r = 10 - 2 * 3;
    printf("r = %d\n", r);
    return 0;
}

Associativity

When operators share precedence, associativity decides direction. Most arithmetic is left-to-right.

#include <stdio.h>
int main(void) {
    int r = 20 / 4 / 2;
    printf("r = %d\n", r);
    return 0;
}

Right Associativity of Assignment

Assignment groups right to left. That is why a = b = 5 assigns 5 to b first, then a.

#include <stdio.h>
int main(void) {
    int a, b;
    a = b = 5;
    printf("a = %d, b = %d\n", a, b);
    return 0;
}

Comparisons vs Arithmetic

Arithmetic binds tighter than relational operators, so a + b > c computes a + b first.

#include <stdio.h>
int main(void) {
    int a = 2, b = 3, c = 4;
    printf("%d\n", a + b > c);
    return 0;
}

Relational vs Logical

Relational operators bind tighter than &&, which binds tighter than ||.

#include <stdio.h>
int main(void) {
    int x = 5;
    if (x > 0 && x < 10)
        printf("single digit positive\n");
    return 0;
}

Unary Before Binary

Unary operators like ! and unary - bind very tightly, applying before binary operators.

#include <stdio.h>
int main(void) {
    int x = 0;
    printf("%d\n", !x + 1);
    return 0;
}

A Tricky Expression

Without parentheses this expression mixes several precedence levels. Trace it carefully.

#include <stdio.h>
int main(void) {
    int r = 2 + 3 * 4 > 10 && 1;
    printf("r = %d\n", r);
    return 0;
}

When in Doubt, Parenthesize

Good C code uses parentheses liberally. They cost nothing at runtime and prevent subtle bugs.

#include <stdio.h>
int main(void) {
    int a = 1, b = 2, c = 3;
    if ((a < b) && (b < c))
        printf("ascending\n");
    return 0;
}

Precedence in Practice

Here precedence lets us write a clean formula without extra parentheses.

#include <stdio.h>
int main(void) {
    int base = 10, rate = 2, hours = 3;
    int pay = base + rate * hours;
    printf("pay = %d\n", pay);
    return 0;
}

Quick Check

Test your understanding of operator precedence.

Recap: Operator Precedence

You learned how C orders operators.

  • * / % before + -
  • Arithmetic before relational before logical
  • Most operators are left-associative, assignment is right-associative
  • Unary operators bind tightly
  • Parentheses make intent explicit

Frequently asked questions

Is the “Operator Precedence” lesson free?

Yes — the full text of “Operator Precedence” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.

What will I learn in “Operator Precedence”?

Evaluation order. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Operator Precedence” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Arithmetic Operators
  2. Relational and Logical
  3. Assignment and Increment
  4. Operator Precedence
← Back to C Academy