Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.
C Operator Precedence Table
The complete 15-level operator precedence table of C, explained with worked examples and the traps that break embedded C programming code.
Welcome to the next lecture of our free C programming course. Every line of C you write, from a = b + c * d to a hardware status-register check inside a driver, is first broken into pieces by the compiler using one rule book: operator precedence.
If you are following this free C tutorial to prepare for embedded C programming work, this lecture matters more than it looks, because precedence mistakes compile cleanly and then fail quietly on real hardware. We will study the full table, evaluate three expressions by hand, then compile a short program that exposes the traps. Everything here applies to C17 and C23, because the table has not changed.
What You Will Learn in This Free C Programming Course Lecture
- What operator precedence means and why the compiler needs it
- All 15 levels of the C operator precedence table, tightest to loosest
- How to read the table and how to evaluate expressions such as
x = a + b < cby hand - Five precedence traps that cause real bugs in embedded C programming
- How to let GCC warn you with
-Wall -Wextra
Prerequisites
- The arithmetic, relational, logical and assignment operators from the earlier Chapter 4 lectures
- Comfort compiling a small C file with
gccon Linux, WSL or macOS
What Is Operator Precedence in C?
Take the expression 2 + 3 * 4. A human can group it as (2 + 3) * 4 and get 20, or as 2 + (3 * 4) and get 14. A compiler cannot guess, so the C language gives every operator a priority. The operator with the higher priority grabs its operands first. Multiplication outranks addition, so the answer is 14.
This priority ranking is called operator precedence. C arranges its operators into 15 levels. Level 1 binds the tightest and level 15, the comma operator, binds the loosest. Be careful when you read other books or websites: some number the levels in the opposite direction, so always check which end is which before comparing tables.
Precedence is about grouping, not timing
Precedence tells the compiler which operands belong to which operator. It does not promise which sub-expression the CPU computes first. In f() + g() * h() the multiplication is grouped first, but C does not say whether f, g or h is called first. We come back to this in the associativity lecture.
C Operator Precedence Table: All 15 Levels
Keep this table open in a second tab while you practise. It is the single most useful reference in this free C tutorial chapter.
| Level | Operators | Description | Associativity |
|---|---|---|---|
| 1 | () [] -> . x++ x-- | Function call, array subscript, arrow, dot, postfix increment and decrement | Left to Right |
| 2 | + - ++x --x ! ~ * & (type) sizeof | Unary plus and minus, prefix increment and decrement, logical NOT, one’s complement, indirection, address-of, cast, size in bytes | Right to Left |
| 3 | * / % | Multiplication, division, modulus | Left to Right |
| 4 | + - | Addition, subtraction | Left to Right |
| 5 | << >> | Left shift, right shift | Left to Right |
| 6 | < <= > >= | Relational comparisons | Left to Right |
| 7 | == != | Equal to, not equal to | Left to Right |
| 8 | & | Bitwise AND | Left to Right |
| 9 | ^ | Bitwise XOR | Left to Right |
| 10 | | | Bitwise OR | Left to Right |
| 11 | && | Logical AND | Left to Right |
| 12 | || | Logical OR | Left to Right |
| 13 | ?: | Conditional (ternary) operator | Right to Left |
| 14 | = += -= *= /= %= &= ^= |= <<= >>= | Assignment operators | Right to Left |
| 15 | , | Comma operator | Left to Right |
Postfix versus prefix increment
Older textbooks list ++ and -- only at level 2. In the C standard, the postfix forms x++ and x-- sit at level 1 and the prefix forms sit at level 2. That small difference explains why *p++ moves the pointer and does not bump the value it points to.
How to Read the Table
- Find the two operators that sit next to each other in your expression.
- Look up their levels. The smaller level number wins and takes its operands first.
- If both operators share a level, the associativity column breaks the tie (the next lecture covers this fully).
- Repeat until the whole expression is grouped, then compute from the inside out.
A quick memory trick for the middle of the table: math first, then shifts, then comparisons, then bit logic, then true/false logic, then assignment. If you remember that sentence you already know levels 3 to 14 in order.
Three Expressions Evaluated by Hand
Example 1: Arithmetic Before Comparison Before Assignment
Let a = 3, b = 4, c = 9 and evaluate x = a + b < c. Addition is level 4, relational is level 6 and assignment is level 14, so the order is add, compare, assign.
Example 2: Why Compound Assignment Waits for the Right Side
Let m = 5, p = 2, q = 6 and evaluate m *= p + q. The compound assignment sits at level 14, so everything on its right is computed first. The statement means m = m * (p + q), not m = m * p + q.
Example 3: Two Comparisons Joined by a Logical OR
Let s = 7, t = 3, u = 3 and evaluate y = s <= t || t == u. Relational (level 6) beats equality (level 7), and both beat logical OR (level 12).
Run It: Precedence Traps in Embedded C Programming
The program below packs the classic mistakes into one file. Save it as ep_prec.c. Every line prints the unparenthesised version first and the corrected version after it.
#include <stdio.h>
int main(void)
{
int a = 3, b = 4, c = 9;
int x = a + b < c;
printf("x = a + b < c -> %d\n", x);
int m = 5, p = 2, q = 6;
m *= p + q;
printf("m *= p + q -> %d\n", m);
int s = 7, t = 3, u = 3;
int y = s <= t || t == u;
printf("y = s <= t || t == u -> %d\n", y);
unsigned int reg = 0x2C, mask = 0x0C, want = 0x0C;
printf("reg & mask == want -> %u\n", reg & mask == want);
printf("(reg & mask) == want -> %d\n", (reg & mask) == want);
printf("1 << 2 + 3 -> %d\n", 1 << 2 + 3);
printf("(1 << 2) + 3 -> %d\n", (1 << 2) + 3);
int arr[3] = {10, 20, 30};
int *ptr = arr;
int v = *ptr++;
printf("v = *ptr++ -> v=%d, *ptr=%d\n", v, *ptr);
(*ptr)++;
printf("(*ptr)++ -> arr[1]=%d\n", arr[1]);
return 0;
}
Build it with warnings switched on and run it:
gcc -std=c17 -Wall -Wextra -o ep_prec ep_prec.c
./ep_prec
GCC already complains about two of the lines. Read these warnings; they are free code review:
ep_prec.c: In function 'main':
ep_prec.c:18:55: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
ep_prec.c:21:46: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
Expected output:
x = a + b < c -> 1
m *= p + q -> 40
y = s <= t || t == u -> 1
reg & mask == want -> 0
(reg & mask) == want -> 1
1 << 2 + 3 -> 32
(1 << 2) + 3 -> 7
v = *ptr++ -> v=10, *ptr=20
(*ptr)++ -> arr[1]=21
What the Output Is Telling You
reg & mask == wantis read asreg & (mask == want). Sincemask == wantis 1, the code computes0x2C & 1, which is 0. The check silently fails even though the bits are set.1 << 2 + 3is1 << (2 + 3), which is 32. If you wanted 7 you needed the parentheses.*ptr++reads the value, then moves the pointer. The value 10 is returned andptrnow points to 20. To increment the value instead, write(*ptr)++.
Five Precedence Traps Every Embedded C Programmer Meets
| You wrote | C reads it as | Safer version |
|---|---|---|
reg & mask == want | reg & (mask == want) | (reg & mask) == want |
1 << 2 + 3 | 1 << (2 + 3) | (1 << 2) + 3 |
*p++ | *(p++) | (*p)++ when you want the value bumped |
a > b ? a : b + 1 | a > b ? a : (b + 1) | (a > b ? a : b) + 1 |
a || b && c | a || (b && c) | Add parentheses so the intent is visible |
Real-World Uses in Embedded C Programming
- Status register checks:
if ((UART_SR & RXNE_FLAG) != 0)needs its parentheses because!=outranks&. - Packing bytes:
value = (hi << 8) | lo;is safe because shift outranks bitwise OR, but the parentheses document the intent. - Flag combinations:
if (ready && !busy || force)mixes AND and OR; parentheses stop the next engineer from misreading it. - Pointer walking:
*buf++ = byte;is the idiomatic write-and-advance pattern in drivers and protocol stacks.
Common Mistakes and Troubleshooting
- The condition never fires. Print the pieces separately:
printf("%u", reg & mask)andprintf("%d", mask == want). If the second line is not what you expected, precedence is the culprit. - Assuming bitwise operators bind like arithmetic. They sit below the comparison operators. Always parenthesise
&,|and^inside conditions. - Memorising a different numbering. If another source says level 1 is the loosest, convert before you trust it.
- Ignoring compiler warnings. Compile with
-Wall -Wextraand treat-Wparenthesesmessages as bugs until proven otherwise.
Best Practices, Performance and Security
- Parenthesise whenever you mix operators from different families: bitwise with relational, shifts with arithmetic, logical AND with logical OR.
- Keep plain arithmetic such as
a + b * cunparenthesised; every reader already knows it. - Coding standards such as MISRA C ask you to make operator precedence explicit in expressions that are not obvious.
Performance
Precedence is resolved at compile time. Adding parentheses changes how the compiler groups your expression, not how fast it runs; the generated machine code for an already-correct expression is identical.
Security
A wrongly grouped permission or status check can be always false or always true. In firmware that gates access to a flash region or a debug port, that is a security hole, not just a bug.
Key Takeaways from This Free C Programming Course Lecture
- C has 15 precedence levels; level 1 binds tightest and the comma operator at level 15 binds loosest.
- Order of grouping: arithmetic, shifts, relational, equality, bitwise AND, XOR, OR, logical AND, OR, conditional, assignment, comma.
- Bitwise operators rank below comparison operators, which is the source of most register-check bugs.
- Postfix
++is level 1 and prefix++is level 2. - Parentheses and compiler warnings are your two best defences.
Conclusion: Continue the Free C Programming Course
The precedence table looks like something to memorise, but you only need its shape: primary operators, unary operators, arithmetic, shifts, comparisons, bit logic, true/false logic, then assignment. Once that shape is in your head, expressions stop being puzzles. This lecture of our free C programming course gave you the full table, three hand-evaluated expressions and the five traps that appear again and again in embedded C programming. In the next lecture we deal with the question precedence cannot answer: what happens when two operators share the same level.
Topics Covered in This Lecture
Interview Questions on This Topic
Which has higher precedence in C, & or ==?
The equality operator == (level 7) outranks bitwise AND & (level 8). So a & b == c means a & (b == c).
What does *p++ do?
Postfix ++ has higher precedence than unary *, so it means *(p++): read the value, then advance the pointer.
Does precedence decide which operand is evaluated first?
No. It decides grouping only. The order in which operands are evaluated is unspecified for most operators.
Where does the assignment operator sit in the table?
Level 14, only above the comma operator. The whole right-hand side is evaluated before the assignment happens.
Why do embedded coding standards demand explicit parentheses?
Because mixed bitwise, relational and logical expressions are easy to misread, and the bug is invisible to the compiler.
Frequently Asked Questions
What is operator precedence in C?
Operator precedence is the rule set that decides which operator groups with its operands first in an expression. C defines 15 levels, and multiplication, for example, outranks addition.
Is there a free C programming course for embedded systems beginners?
Yes. This free C programming course on EmbeddedPathashala covers C from the basics to embedded C programming, with runnable examples and interview questions in every lecture.
Which operators have the lowest precedence in C?
The assignment operators sit at level 14 and the comma operator at level 15, so they are applied last.
Do parentheses slow my program down?
No. Parentheses only change how the compiler groups the expression. They do not add runtime cost.
Why does reg & mask == value give the wrong answer?
Because == binds tighter than &, so C evaluates mask == value first and then ANDs the result with reg. Write (reg & mask) == value.
Is the precedence table the same in C17 and C23?
Yes. The operator precedence and associativity rules are unchanged, so the table in this lecture applies to current compilers.
Keep Learning with the Free C Programming Course
Next: associativity, the tie-breaker that decides how operators of the same level group, from subtraction chains to chained assignment.
Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments