Operator precedence determines the order in which operators are evaluated when an expression contains more than one operator. Understanding precedence helps you predict the result of an expression correctly.
Operator precedence is the set of rules that determines which operator should be evaluated first in an expression.
int result = 10 + 5 * 2;
Multiplication has higher precedence than addition, so the expression is evaluated as:
10 + (5 * 2)
The result is:
20
When an expression contains several operators, C needs rules to determine the order in which those operators are evaluated.
int result = 20 - 10 / 2;
Division is performed before subtraction:
20 - (10 / 2)
Therefore:
result = 15;
A simple rule to remember is:
int result = (10 + 5) * 2;
The expression inside parentheses is evaluated first:
(10 + 5) = 15
Then:
15 * 2 = 30
Therefore, the result is 30.
int result = 5 + 4 * 3;
Multiplication is performed first:
4 * 3 = 12
Then:
5 + 12 = 17
So the result is 17.
int result = 30 - 20 / 5;
Division is evaluated first:
20 / 5 = 4
Then:
30 - 4 = 26
Therefore, the result is 26.
int result = 10 + 7 % 3;
The remainder operator has the same precedence level as multiplication and division.
7 % 3 = 1
Then:
10 + 1 = 11
So the result is 11.
The operators *, /, and % have the
same precedence level.
int result = 20 / 5 * 3;
Operators with the same precedence are generally evaluated according to their associativity. These arithmetic operators are evaluated from left to right.
(20 / 5) * 3
Result:
12
The + and - arithmetic operators have the same
precedence.
int result = 20 - 5 + 3;
They are evaluated from left to right:
(20 - 5) + 3
Result:
18
When operators have the same precedence and the operators are left associative, evaluation proceeds from left to right.
int result = 100 / 10 * 2;
This becomes:
(100 / 10) * 2
Result:
20
Unary operators work with one operand. Examples include:
++--!-+int x = 5;
int result = -x + 10;
The unary minus is applied to x before the addition.
Relational operators include:
< > <= >=
They are generally evaluated after arithmetic operations.
int result = 5 + 3 > 7;
The addition is performed first:
8 > 7
The condition is true, so the result is typically 1.
Equality operators are:
==!=
They have lower precedence than relational operators.
int result = 5 < 10 == 1;
The relational expression is evaluated first:
(5 < 10) == 1
This results in true, so result becomes 1.
The logical AND operator is &&.
It has lower precedence than equality and relational operators.
int result = 10 > 5 && 20 > 10;
The relational expressions are evaluated first:
(10 > 5) && (20 > 10)
Both conditions are true, so the result is 1.
The logical OR operator is ||. It has lower precedence than
logical AND.
int result = 10 > 5 || 20 < 10;
The comparisons are evaluated first:
(10 > 5) || (20 < 10)
Since the first condition is true, the result is 1.
The logical AND operator has higher precedence than logical OR.
int result = 1 || 0 && 0;
The AND operation is evaluated first:
1 || (0 && 0)
Then:
1 || 0
The final result is 1.
Assignment operators such as =, +=,
-=, and *= have relatively low precedence.
int x;
x = 10 + 5 * 2;
The arithmetic expression is evaluated first:
10 + (5 * 2)
Then the result is assigned to x.
int result1 = 10 + 5 * 2;
int result2 = (10 + 5) * 2;
First expression:
10 + (5 * 2) = 20
Second expression:
(10 + 5) * 2 = 30
Parentheses make the intended order explicit.
Precedence determines which operator is considered first when different precedence levels are involved.
Associativity determines the direction of evaluation when operators have the same precedence.
20 / 5 * 2
Both / and * have the same precedence and are
left associative, so the expression is evaluated as:
(20 / 5) * 2
The conditional operator ?: has lower precedence than logical
OR and higher precedence than assignment operators.
int result = 10 > 5 ? 100 : 200;
The relational comparison is evaluated before the conditional operator.
For readability, parentheses can be used:
int result = (10 > 5) ? 100 : 200;
int age = 20;
int status = (age >= 18) ? 1 : 0;
The comparison is evaluated first, then the conditional operator selects
the appropriate value, and finally the result is assigned to
status.
int result = 10 + 5 * 2 > 15;
Step 1: Multiplication:
5 * 2 = 10
Step 2: Addition:
10 + 10 = 20
Step 3: Comparison:
20 > 15
The result is 1.
Even when you know the precedence rules, parentheses can make an expression easier for other programmers to understand.
int result = (a + b) * (c - d);
This clearly shows that both additions and subtractions inside the parentheses should be completed before multiplication.
A simplified order from higher to lower precedence is:
This is a simplified learning order. The complete C operator precedence table contains additional operators and associativity rules.
int result = 10 + 20 / 5;
A beginner may calculate it as:
(10 + 20) / 5
But that is not how C evaluates the expression.
Division has higher precedence:
10 + (20 / 5)
Result:
14
The assignment operator = is different from the equality
operator ==.
int x = 10;
if (x == 10)
{
printf("Equal");
}
Use = to assign a value and == to compare values.
#include <stdio.h>
int main()
{
int a = 10;
int b = 5;
int c = 2;
int result = a + b * c;
printf("Result = %d", result);
return 0;
}
The expression is evaluated as:
10 + (5 * 2)
Therefore:
Result = 20
#include <stdio.h>
int main()
{
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
int result = a + b * 2;
printf("Result = %d", result);
return 0;
}
Multiplication is performed before addition in the expression
a + b * 2.
int total = (price * quantity) + tax;
Parentheses make the calculation easier to understand.
Find the result of each expression:
10 + 5 * 220 - 10 / 2(10 + 5) * 220 / 5 * 210 + 7 % 35 + 3 > 710 > 5 && 20 > 101 || 0 && 010 + 5 * 2 > 15(10 + 5) > 12 ? 100 : 200Try to solve each expression step by step using operator precedence before checking the answer.
*, /, and % are left associative.Question: Which operator is evaluated before the addition operator in the expression 10 + 5 * 2?