← Back to C Course | Chapter 3: Operators | Lesson 9 of 9

C Operator Precedence

What is Precedence?

Operator precedence is the fixed order C uses to decide which operator in a mixed expression gets evaluated first, following the same logic as algebra's "multiply before you add" rule you'd apply by hand.

Example: What is Precedence?

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

Operator Associativity

When two operators in the same expression share equal precedence, associativity decides the tie: most C operators associate left-to-right, but a few (like assignment) associate right-to-left instead.

Example: Operator Associativity

c
#include <stdio.h>
int main() {
	int x, y;
	x = y = 10;
	printf("%d %d", x, y);
	return 0;
}

Arithmetic Hierarchy

*, /, and % all share the same precedence level and are evaluated before + and -, so 2 + 3 * 4 evaluates the multiplication first, giving 14 rather than 20.

Example: Arithmetic Hierarchy

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

Using Parentheses

Parentheses always take priority over C's default precedence rules, so wrapping part of an expression in () forces it to be evaluated first -- the clearest way to make your intended order of operations explicit rather than relying on memorized rules.

Example: Using Parentheses

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

Relational and Logical Priority

Arithmetic operators bind tighter than relational operators, which in turn bind tighter than logical operators, so a + b > c && d evaluates the addition, then the comparison, then the logical AND, in that order.

Example: Relational and Logical Priority

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

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.