C Operator Precedence
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- Assuming
a + b * cis evaluated left to right, when*runs before+. - Writing
a & b == c, where==binds tighter than&. - Not using parentheses to make the intended order clear.
- 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.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: