</>
Skip to content
C lessons (10/28)

C — Operators

Arithmetic operators

#include <stdio.h>

int main() {
    int a = 10, b = 3;

    printf("Add: %d\n", a + b);       // 13
    printf("Sub: %d\n", a - b);       // 7
    printf("Mul: %d\n", a * b);       // 30
    printf("Div: %d\n", a / b);       // 3 (integer division!)
    printf("Mod: %d\n", a % b);       // 1 (remainder)

    // Integer division truncates
    printf("10 / 3 = %d\n", 10 / 3);       // 3
    printf("10.0 / 3 = %.2f\n", 10.0 / 3); // 3.33

    return 0;
}

Increment and decrement

#include <stdio.h>

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

    // Post-increment: use then increment
    printf("a++ = %d, a is now %d\n", a++, a); // 5, 6

    // Pre-increment: increment then use
    printf("++b = %d\n", ++b); // 6

    // Same for decrement
    printf("a-- = %d, a is now %d\n", a--, a); // 6, 5
    printf("--b = %d\n", --b); // 5

    return 0;
}

Bitwise operators

#include <stdio.h>

int main() {
    unsigned int a = 0b1010; // 10
    unsigned int b = 0b1100; // 12

    printf("AND: %u\n", a & b);   // 8  (1000)
    printf("OR:  %u\n", a | b);   // 14 (1110)
    printf("XOR: %u\n", a ^ b);   // 6  (0110)
    printf("NOT: %u\n", ~a);       // Large number (bitwise NOT)
    printf("Left:  %u\n", a << 1); // 20 (10100)
    printf("Right: %u\n", a >> 1); // 5  (101)

    return 0;
}

Logical operators

#include <stdio.h>

int main() {
    int a = 1, b = 0;

    printf("AND: %d\n", a && b);  // 0 (false)
    printf("OR:  %d\n", a || b);  // 1 (true)
    printf("NOT: %d\n", !a);      // 0 (false)

    // Short-circuit evaluation
    int x = 0;
    // This is safe because && short-circuits
    if (x != 0 && 10 / x > 2) {
        printf("Never reached\n");
    }

    return 0;
}

Relational operators

#include <stdio.h>

int main() {
    int a = 5, b = 10;

    printf("Equal:       %d\n", a == b);  // 0
    printf("Not equal:   %d\n", a != b);  // 1
    printf("Greater:     %d\n", a > b);   // 0
    printf("Less:        %d\n", a < b);   // 1
    printf("Greater eq:  %d\n", a >= b);  // 0
    printf("Less eq:     %d\n", a <= b);  // 1

    return 0;
}

Assignment operators

#include <stdio.h>

int main() {
    int x = 10;

    x += 5;   // x = 15
    x -= 3;   // x = 12
    x *= 2;   // x = 24
    x /= 4;   // x = 6
    x %= 4;   // x = 2
    x <<= 1;  // x = 4
    x >>= 1;  // x = 2
    x &= 0b1111; // x = 2
    x |= 0b1000; // x = 10
    x ^= 0b1111; // x = 5

    printf("Final: %d\n", x);
    return 0;
}

Ternary operator

#include <stdio.h>

int main() {
    int age = 20;
    const char *status = (age >= 18) ? "Adult" : "Minor";
    printf("Status: %s\n", status); // Adult

    int a = 5, b = 10;
    int max = (a > b) ? a : b;
    printf("Max: %d\n", max); // 10

    return 0;
}

sizeof operator

#include <stdio.h>

int main() {
    printf("char:     %zu\n", sizeof(char));     // 1
    printf("int:      %zu\n", sizeof(int));      // 4
    printf("double:   %zu\n", sizeof(double));   // 8
    printf("pointer:  %zu\n", sizeof(void *));   // 4 or 8

    int arr[] = {1, 2, 3, 4, 5};
    printf("array:    %zu\n", sizeof(arr));       // 20
    printf("elements: %zu\n", sizeof(arr) / sizeof(arr[0])); // 5

    return 0;
}

Comma operator

Evaluates expressions left to right, returns the rightmost value:

#include <stdio.h>

int main() {
    int x = (1, 2, 3); // x = 3
    printf("x = %d\n", x);

    // Common in for loops
    for (int i = 0, j = 10; i < j; i++, j--) {
        printf("i=%d j=%d ", i, j);
    }
    printf("\n");

    return 0;
}

Operator precedence

From highest to lowest:

PrecedenceOperatorsDescription
1() [] -> .Parentheses, array, member
2! ~ ++ -- + - * & sizeofUnary
3* / %Multiplicative
4+ -Additive
5<< >>Bitwise shift
6< <= > >=Relational
7== !=Equality
8&Bitwise AND
9^Bitwise XOR
10|Bitwise OR
11&&Logical AND
12||Logical OR
13?:Ternary
14= += -= etc.Assignment

Always use parentheses when precedence is unclear.

Mini Practice

Write C code that:

  1. Demonstrates integer division vs floating-point division
  2. Uses bitwise operators to set and clear individual bits
  3. Shows short-circuit evaluation with logical operators
  4. Uses the ternary operator to find the maximum of three numbers

Up Next

In the next lesson, you'll learn about If Else — conditional branching in C.

Related Topics

Frequently Asked Questions about Operators

What is Operators in C?

Operators is a fundamental concept in C. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Operators?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Operators.

Why is Operators important in C?

Operators is essential for C development. Understanding this concept will help you write better code and solve real-world problems more effectively.