Operators and Precedence in C: Step-by-Step Tutorial with Runnable Examples

Learn how C groups arithmetic, relational, logical, bitwise, conditional, and assignment operators. Trace exact outputs and repair common expression mistakes.

KnowledgeGate Team

Exam prep & CS education

Updated 9 Aug 20266 min read

You may know what +, &&, and = mean separately, yet still predict the wrong result when they appear in one C expression. Use a fixed method: identify the operands, group operators by precedence, apply associativity only within the same level, then check sequencing and operand types. Run that method on 5 + 2 * 3 > 10 && 8 / 4 == 2 and it settles on 1 without a single guess. Aim for a trace you can explain, not blind memorisation.

C operators: operands, results, and the main operator families

An operator is a symbol that acts on one, two, or three operands. !x is unary, a + b is binary, and condition ? yes : no is the ternary conditional operator.

With int a = 12, b = 5, the main families behave like this:

Family

Example

Result

Arithmetic

a + b, a - b, a * b, a / b, a % b

17, 7, 60, 2, 2

Relational

a > b, a == b

1, 0

Logical

a && b, !a

1, 0

Assignment

int c = 3; c += 4;

c = 7

Conditional

a > b ? a : b

12

Relational and logical operators produce integer 0 or 1. Other families include bitwise, increment and decrement, address and dereference, member access, cast, sizeof, and comma operators. For a wider C-to-DSA path, follow the sequence under Coding & DSA courses.

Operator precedence in C: grouping rules from postfix to comma

Read these precedence tiers from high to low:

Tier

Operators

Postfix

() [] -> . x++ x--

Unary and cast

++ -- + - ! ~ * & sizeof (type)

Multiplicative, additive

* / %, then + -

Shift, relational, equality

<< >>, then < <= > >=, then == !=

Bitwise AND, XOR, OR

&, then ^, then |

Logical AND, OR

&&, then ||

Conditional

?:

Assignment

= += -= *= /= %= <<= >>= &= ^= |=

Comma

,

Binary tiers generally group left to right; unary, conditional, and assignment right to left. Thus 20 / 5 * 2 is (20 / 5) * 2 = 8, while a = b = 7 is a = (b = 7), leaving both 7.

Precedence describes parsing, not evaluation order. Parentheses show grouping but cannot make unsequenced side effects safe. Learn common tiers; parenthesise unclear intent.

Operator precedence in C: trace one mixed expression completely

Run this program:

#include <stdio.h>

int main(void) {
    int result = 5 + 2 * 3 > 10 && 8 / 4 == 2;
    printf("%d\n", result);
    return 0;
}
  1. 2 * 3 = 6

  2. 5 + 6 = 11

  3. 11 > 10 = 1

  4. 8 / 4 = 2

  5. 2 == 2 = 1

  6. 1 && 1 = 1

Grouping is ((5 + (2 * 3)) > 10) && ((8 / 4) == 2). Output:

1

&& evaluates left first. It evaluates the right here only because the left is true.

Precedence parse tree tracing 5 + 2 * 3 > 10 && 8 / 4 == 2 down to a final result of 1.

Arithmetic, relational, logical, and conditional operators in C

Operand types affect arithmetic. With integers, 5 / 2 gives 2. In (double)5 / 2, the cast converts 5 before division because casts are in the unary tier, so floating-point division gives 2.5.

Relational operators compare and hand back an int. With int p = 7, q = 7;, p > q is 0, p >= q is 1, and p != q is 0. The relational tier sits below both arithmetic tiers, so p + 1 > q groups as (p + 1) > q and gives 1.

Short-circuiting can guard an operation:

int num = 10, den = 0;
int safe = den != 0 && num / den > 2;

den != 0 is false, so num / den is not evaluated and safe becomes 0. Bitwise & does not provide this protection.

The conditional operator selects one of two results. In int a = 14, b = 9; int max = a > b ? a : b;, max becomes 14. Parentheses remain useful when ?: appears inside a larger expression.

Bitwise operators in C: calculate with exact binary values

Let unsigned x = 12u; and unsigned y = 10u;. The rows below show the low eight bits; a real unsigned int is usually 32 bits wide, and for these two values every higher bit is 0. Review Number Systems and Base Conversions Explained if base conversion is unfamiliar.

Expression

Decimal

Eight-bit representation

x

12

00001100

y

10

00001010

x & y

8

00001000

x | y

14

00001110

x ^ y

6

00000110

x << 1

24

00011000

y >> 1

5

00000101

AND, OR, and XOR follow the Boolean Algebra and K-map Minimization Guide. Bitwise 12 & 10 is 8; logical 12 && 10 is 1 since both are nonzero.

Eight-bit grid comparing x = 12 and y = 10 with their AND, OR, XOR, and shift results.

Associativity, increment, assignment, and sequencing in C

Associativity settles ties inside one precedence level and nothing more. 100 / 10 / 5 groups left to right as (100 / 10) / 5 = 2; the other grouping, 100 / (10 / 5), would give 50. Assignment groups the other way, so total = count = 0 runs count = 0 first and then stores that assignment's own value in total. Neither rule fixes the order in which the operands themselves are evaluated.

Keep side effects fully separate here:

#include <stdio.h>

int main(void) {
    int i = 4;
    int old = i++;   /* old = 4, i becomes 5 */
    int now = ++i;   /* i becomes 6, now = 6 */
    printf("%d %d %d\n", old, now, i);
    return 0;
}

Postfix i++ hands back the old value and increments afterwards; prefix ++i increments first and hands back the new value. Output:

4 6 6

i = i++ + ++i; has undefined behaviour because i is modified repeatedly without required sequencing. Precedence groups it, but no portable answer exists.

In int z = (a = 3, b = 4, a + b);, the comma operator runs left to right and leaves z = 7. Function-argument commas are separators.

C operator mistakes and exam-style output exercises

Parse these:

Wrong code

Actual parse or result

Repair

if (x = 0)

Assigns 0; false

if (x == 0)

2 & 1 == 0

2 & (1 == 0) gives 0

(2 & 1) == 0 gives 1

3 < x < 10, x = 20

(3 < 20) < 10, then 1 < 10, gives 1

3 < x && x < 10 gives 0

5 / 2

Integer result: 2

Cast first for 2.5

Predict first:

Expression

Answer

18 - 5 * 2 + 18 % 5

18 - 10 + 3 = 11

4 + 6 / 3 * 2

4 + 2 * 2 = 8

(12 & 10) == 8 && (12 ^ 10) == 6

1 && 1 = 1

At n = 9: 3 < n < 8 versus 3 < n && n < 8

Chained: 1 < 8 = 1; logical: 1 && 0 = 0

After int a, b; a = b = 6;

a = 6, b = 6

Exam setters reuse a small set of shapes: predict an output, add the parentheses that repair a misparse, catch = written where == was meant, separate bitwise & from logical &&, and recognise an expression that is undefined instead of computing one. For the full rung-by-rung ladder with an associativity column, and the a+++b tokenisation trap, read Operator Precedence and Associativity in C.

Operators and precedence in C: the short version and next step

Use four steps: mark operators, group by precedence, apply associativity within one tier, then check types and sequencing. Do not guess from surface order. Explicit parentheses document the intended grouping clearly, but do not fix unsafe side effects.

Retype the mixed-expression program, change one operator at a time, and predict the output before compiling. Explain each grouping before accepting the result. For a complete progression from operators into control flow, functions, arrays, and pointers, continue with the C Language course.