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

C Operator Precedence

Operator precedence is the order C follows when a line has several operators, like doing multiplication before addition in school. Brackets let you decide the order yourself.
Syntax
c
result = a + b * c;    // * / % run before + -
result = (a + b) * c;  // parentheses run first

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 standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare result and set it to 2 + 3 * 4
	int result = 2 + 3 * 4;
	// Print formatted text to the screen
	printf("%d", result);
	// Return 0 to signal that the program finished successfully
	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 standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int x, y;
	x = y = 10;
	// Print formatted text to the screen
	printf("%d %d", x, y);
	// Return 0 to signal that the program finished successfully
	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 standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare result and set it to 2 + 3 * 4
	int result = 2 + 3 * 4;
	// Print formatted text to the screen
	printf("%d", result);
	// Return 0 to signal that the program finished successfully
	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 standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare result and set it to (2 + 3) * 4
	int result = (2 + 3) * 4;
	// Print formatted text to the screen
	printf("%d", result);
	// Return 0 to signal that the program finished successfully
	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 standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int a = 2, b = 3, c = 4, d = 1;
	// Declare result and set it to a + b > c && d
	int result = a + b > c && d;
	// Print formatted text to the screen
	printf("%d", result);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Assuming a + b * c is evaluated left to right, when * runs before +.
  2. Writing a & b == c, where == binds tighter than &.
  3. Not using parentheses to make the intended order clear.
Chapter Summary
  • Operators act on values: arithmetic, relational, logical, bitwise, and assignment operators each serve a different purpose.
  • Increment, decrement, and the ternary operator provide compact ways to change values or choose between two results.
  • Operator precedence decides the order in which a mixed expression is evaluated.

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.