Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 7 of 10

Operators - Arithmetic, Bitwise & Precedence

C has a rich set of operators: arithmetic, relational, logical, bitwise, assignment, and special operators (`sizeof`, ternary, comma). Operator **precedence** and **associativity** determine evaluation order. In C, the single biggest source of precedence mistakes is assuming `&` (address-of or bitwise AND) has higher precedence than `==` - it does not.

12 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

All Operator Categories with Examples

All Operator Categories with Examples
#include <stdio.h>

int main(void) {
    int a = 12, b = 5;

    /* ── Arithmetic ──────────────────────────────────── */
    printf("a+b=%d  a-b=%d  a*b=%d
", a+b, a-b, a*b);
    printf("a/b=%d (int div)  a%%b=%d
", a/b, a%b);

    /* ── Increment / Decrement ───────────────────────── */
    int x = 5;
    printf("x++=%d, x=%d
", x++, x);   /* post: returns 5, then x=6 */
    x = 5;
    printf("++x=%d, x=%d
", ++x, x);   /* pre:  x=6, returns 6      */

    /* ── Relational (returns 0 or 1) ────────────────── */
    printf("a==b:%d  a!=b:%d  a>b:%d  a<=b:%d
",
           a==b, a!=b, a>b, a<=b);

    /* ── Logical ─────────────────────────────────────── */
    int p=1, q=0;
    printf("p&&q=%d  p||q=%d  !p=%d
", p&&q, p||q, !p);
    /* Short-circuit: in p||q, if p is true, q is NOT evaluated */

    /* ── Bitwise ─────────────────────────────────────── */
    unsigned char mask = 0b00001111;  /* C99: binary literal in gcc */
    unsigned char val  = 0b10101010;
    printf("val & mask = %u
",  val & mask);  /* AND: 0000 1010 = 10  */
    printf("val | 0xF0 = %u
",  val | 0xF0);  /* OR:  1111 1010 = 250 */
    printf("val ^ 0xFF = %u
",  val ^ 0xFF);  /* XOR: 0101 0101 = 85  */
    printf("~val       = %d
",  (int)(unsigned char)~val); /* NOT */
    printf("val << 2   = %u
",  val << 2);    /* left shift  */
    printf("val >> 1   = %u
",  val >> 1);    /* right shift */

    /* ── Ternary ─────────────────────────────────────── */
    int max = (a > b) ? a : b;
    printf("max(%d,%d) = %d
", a, b, max);

    /* ── sizeof - compile-time, NOT a function call ──── */
    printf("sizeof(int) = %zu
", sizeof(int));
    printf("sizeof(a)   = %zu
", sizeof(a));  /* same as sizeof(int) */

    /* ── Precedence trap ─────────────────────────────── */
    int flags = 0b1010;
    /* WRONG: == has higher precedence than &  */
    if (flags & 0b0010 == 0b0010)   /* parsed as: flags & (0b0010 == 0b0010) */
        printf("bit set (WRONG check)
");
    /* CORRECT: always parenthesise bitwise checks */
    if ((flags & 0b0010) == 0b0010)
        printf("bit 1 is set (correct)
");

    return 0;
}

Common Mistakes

  • The `&` vs `==` precedence trap - `flag & MASK == VALUE` is parsed as `flag & (MASK == VALUE)`. Always wrap bitwise operations in parentheses when comparing: `(flag & MASK) == VALUE`.
  • Shifting by more than the type width - `1 << 32` on a 32-bit `int` is undefined behaviour. The shift amount must be less than the bit width of the promoted type.
  • Modifying a variable twice in the same expression - `a[i++] = i` is undefined behaviour in C (the order of side effects is unspecified). Never modify and read the same variable in one expression without a sequence point.

Tip

Tip

Practice Operators Arithmetic Bitwise Precedence in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Operator precedence and associativity dictate exact expression evaluation order

Practice Task

Note

Practice Task - (1) Write a working example of Operators Arithmetic Bitwise Precedence from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Operators Arithmetic Bitwise Precedence is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.

Key Takeaways

  • C has a rich set of operators: arithmetic, relational, logical, bitwise, assignment, and special operators (`sizeof`, ternary, comma).
  • The `&` vs `==` precedence trap - `flag & MASK == VALUE` is parsed as `flag & (MASK == VALUE)`. Always wrap bitwise operations in parentheses when comparing: `(flag & MASK) == VALUE`.
  • Shifting by more than the type width - `1 << 32` on a 32-bit `int` is undefined behaviour. The shift amount must be less than the bit width of the promoted type.
  • Modifying a variable twice in the same expression - `a[i++] = i` is undefined behaviour in C (the order of side effects is unspecified). Never modify and read the same variable in one expression without a sequence point.

Topics in This Module

0/10 done