Module 3 of 13 · C Programming Fundamentals · Beginner

Operators & Expressions

Duration: 50 min

Operators are symbols that perform operations on variables and values. C provides arithmetic, relational, logical, and bitwise operators. Mastering operators is essential for writing effective C programs.

Arithmetic Operators

#include <stdio.h>

int main() {
    int a = 10, b = 3;
    
    printf("Addition: %d + %d = %d\n", a, b, a + b);      // 13
    printf("Subtraction: %d - %d = %d\n", a, b, a - b);   // 7
    printf("Multiplication: %d * %d = %d\n", a, b, a * b); // 30
    printf("Division: %d / %d = %d\n", a, b, a / b);      // 3 (integer division)
    printf("Modulo: %d %% %d = %d\n", a, b, a % b);       // 1 (remainder)
    
    return 0;
}

Relational Operators

#include <stdio.h>

int main() {
    int x = 5, y = 10;
    
    printf("x == y: %d\n", x == y);  // 0 (false)
    printf("x != y: %d\n", x != y);  // 1 (true)
    printf("x < y: %d\n", x < y);    // 1 (true)
    printf("x > y: %d\n", x > y);    // 0 (false)
    printf("x <= y: %d\n", x <= y);  // 1 (true)
    printf("x >= y: %d\n", x >= y);  // 0 (false)
    
    return 0;
}

Logical Operators

#include <stdio.h>

int main() {
    int a = 1, b = 0;
    
    printf("a && b (AND): %d\n", a && b);    // 0 (false)
    printf("a || b (OR): %d\n", a || b);     // 1 (true)
    printf("!a (NOT): %d\n", !a);            // 0 (false)
    printf("!b (NOT): %d\n", !b);            // 1 (true)
    
    // Practical example
    int age = 25, has_license = 1;
    if (age >= 18 && has_license) {
        printf("Can drive\n");
    }
    
    return 0;
}

Bitwise Operators

#include <stdio.h>

int main() {
    int a = 5;      // Binary: 0101
    int b = 3;      // Binary: 0011
    
    printf("a & b (AND): %d\n", a & b);     // 1 (0001)
    printf("a | b (OR): %d\n", a | b);      // 7 (0111)
    printf("a ^ b (XOR): %d\n", a ^ b);     // 6 (0110)
    printf("~a (NOT): %d\n", ~a);           // -6
    printf("a << 1 (Left shift): %d\n", a << 1);   // 10 (1010)
    printf("a >> 1 (Right shift): %d\n", a >> 1);  // 2 (0010)
    
    return 0;
}

Assignment & Compound Operators

#include <stdio.h>

int main() {
    int x = 10;
    
    x += 5;     // x = x + 5;  → 15
    printf("After += 5: %d\n", x);
    
    x -= 3;     // x = x - 3;  → 12
    printf("After -= 3: %d\n", x);
    
    x *= 2;     // x = x * 2;  → 24
    printf("After *= 2: %d\n", x);
    
    x /= 4;     // x = x / 4;  → 6
    printf("After /= 4: %d\n", x);
    
    x %= 4;     // x = x % 4;  → 2
    printf("After %%= 4: %d\n", x);
    
    return 0;
}

Increment & Decrement Operators

#include <stdio.h>

int main() {
    int a = 5;
    
    // Pre-increment: increment first, then use
    printf("++a: %d\n", ++a);  // 6
    
    // Post-increment: use first, then increment
    printf("a++: %d\n", a++);  // 6
    printf("a after post-increment: %d\n", a);  // 7
    
    // Pre-decrement
    printf("--a: %d\n", --a);  // 6
    
    // Post-decrement
    printf("a--: %d\n", a--);  // 6
    printf("a after post-decrement: %d\n", a);  // 5
    
    return 0;
}

Operator Precedence

#include <stdio.h>

int main() {
    // Multiplication and division before addition and subtraction
    int result1 = 2 + 3 * 4;  // 14 (not 20)
    printf("2 + 3 * 4 = %d\n", result1);
    
    // Use parentheses for clarity
    int result2 = (2 + 3) * 4;  // 20
    printf("(2 + 3) * 4 = %d\n", result2);
    
    // Precedence: () > * / % > + - > < > <= >= > == != > && > ||
    int result3 = 5 > 3 && 2 < 4;  // 1 (true)
    printf("5 > 3 && 2 < 4 = %d\n", result3);
    
    return 0;
}

Quiz 1: Arithmetic Operators

❓ What is the result of 17 % 5?

Quiz 2: Logical Operators

❓ What is the result of (1 && 0) || 1?

Quiz 3: Bitwise Operators

❓ What is 5 & 3 in binary (5 = 0101, 3 = 0011)?

Quiz 4: Operator Precedence

❓ What is the result of 2 + 3 * 4 - 1?

Quiz 5: Increment Operators

❓ What is the difference between ++x and x++?

← Previous Continue interactively → Next →

Related Courses