Day 4: Operators & Expressions#

Overview#

Operators are symbols that tell the compiler to perform specific mathematical or logical manipulations. Today we’ll learn all the operators available in C and how to use them effectively.

What We’ll Learn Today#

  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Bitwise operators
  • Assignment operators
  • Type casting in expressions
  • Operator precedence and associativity

Arithmetic Operators#

Used for mathematical calculations:

Basic Arithmetic Operators#

OperatorNameExampleResult
+Addition10 + 515
-Subtraction10 - 55
*Multiplication10 * 550
/Division10 / 52
%Modulus (remainder)10 % 31

Example Program#

#include <stdio.h>

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

    printf("Addition: %d + %d = %d\n", a, b, a + b);
    printf("Subtraction: %d - %d = %d\n", a, b, a - b);
    printf("Multiplication: %d * %d = %d\n", a, b, a * b);
    printf("Division: %d / %d = %d\n", a, b, a / b);
    printf("Modulus: %d %% %d = %d\n", a, b, a % b);

    return 0;
}

Output:

Addition: 20 + 5 = 25
Subtraction: 20 - 5 = 15
Multiplication: 20 * 5 = 100
Division: 20 / 5 = 4
Modulus: 20 % 3 = 2

Important Notes on Division#

// Integer division (truncates decimal)
int result1 = 7 / 2;      // Result: 3
printf("%d\n", result1);

// Float division (preserves decimal)
double result2 = 7.0 / 2;  // Result: 3.5
printf("%.1f\n", result2);

Increment and Decrement Operators#

int x = 5;

x++;     // Post-increment: x = 6
x--;     // Post-decrement: x = 5
++x;     // Pre-increment: x = 6
--x;     // Pre-decrement: x = 5

Difference Between Pre and Post#

int x = 5, y, z;

y = x++;   // y gets 5, then x becomes 6
z = ++x;   // x becomes 7, then z gets 7

printf("x = %d, y = %d, z = %d\n", x, y, z);  // x = 7, y = 5, z = 7

Relational Operators#

Compare two values and return true (1) or false (0):

OperatorMeaningExampleResult
==Equal to5 == 51 (true)
!=Not equal to5 != 31 (true)
>Greater than5 > 31 (true)
<Less than5 < 30 (false)
>=Greater than or equal5 >= 51 (true)
<=Less than or equal5 <= 30 (false)

Example Program#

#include <stdio.h>

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

    printf("a == b: %d\n", a == b);   // 0
    printf("a != b: %d\n", a != b);   // 1
    printf("a > b: %d\n", a > b);     // 1
    printf("a < b: %d\n", a < b);     // 0
    printf("a >= b: %d\n", a >= b);   // 1
    printf("a <= b: %d\n", a <= b);   // 0

    return 0;
}

Important: Use == for comparison, not = (which is assignment)!

// ❌ Wrong - assigns 5 to x
if (x = 5) { }

// ✅ Correct - compares x to 5
if (x == 5) { }

Logical Operators#

Combine conditions for complex decision-making:

OperatorMeaningTruth Table
&&ANDTrue only if BOTH are true
||ORTrue if AT LEAST ONE is true
!NOTInverts the result

AND Operator (&&)#

#include <stdio.h>

int main() {
    int age = 25;
    int score = 80;

    if (age >= 18 && score >= 75) {
        printf("Eligible for admission\n");
    } else {
        printf("Not eligible\n");
    }

    return 0;
}

OR Operator (||)#

#include <stdio.h>

int main() {
    int day = 7;

    if (day == 6 || day == 7) {
        printf("It's the weekend!\n");
    } else {
        printf("It's a weekday\n");
    }

    return 0;
}

NOT Operator (!)#

#include <stdio.h>

int main() {
    int is_raining = 1;  // 1 = true

    if (!is_raining) {
        printf("Go outside!\n");
    } else {
        printf("Stay inside\n");
    }

    return 0;
}

Truth Table Example#

// AND truth table
1 && 1 = 1
1 && 0 = 0
0 && 1 = 0
0 && 0 = 0

// OR truth table
1 || 1 = 1
1 || 0 = 1
0 || 1 = 1
0 || 0 = 0

// NOT truth table
!1 = 0
!0 = 1

Assignment Operators#

Assign values and perform operations simultaneously:

OperatorExampleEquivalent to
=x = 5Assignment
+=x += 5x = x + 5
-=x -= 5x = x - 5
*=x *= 5x = x * 5
/=x /= 5x = x / 5
%=x %= 5x = x % 5

Example#

#include <stdio.h>

int main() {
    int x = 10;

    printf("Initial x: %d\n", x);

    x += 5;    // x = 15
    printf("After x += 5: %d\n", x);

    x -= 3;    // x = 12
    printf("After x -= 3: %d\n", x);

    x *= 2;    // x = 24
    printf("After x *= 2: %d\n", x);

    x /= 4;    // x = 6
    printf("After x /= 4: %d\n", x);

    x %= 4;    // x = 2
    printf("After x %%= 4: %d\n", x);

    return 0;
}

Bitwise Operators#

Work on individual bits. (Advanced - useful for system programming)

OperatorNameExample
&AND5 & 3 = 1
|OR5 | 3 = 7
^XOR5 ^ 3 = 6
~NOT~5 = -6
<<Left shift5 « 1 = 10
>>Right shift5 » 1 = 2

Quick Example#

#include <stdio.h>

int main() {
    int a = 5;      // Binary: 101
    int b = 3;      // Binary: 011

    printf("a & b = %d\n", a & b);     // 1 (001)
    printf("a | b = %d\n", a | b);     // 7 (111)
    printf("a ^ b = %d\n", a ^ b);     // 6 (110)
    printf("~a = %d\n", ~a);           // -6
    printf("a << 1 = %d\n", a << 1);   // 10 (1010)
    printf("a >> 1 = %d\n", a >> 1);   // 2 (10)

    return 0;
}

Operator Precedence and Associativity#

Determines which operations are performed first:

Precedence Order (Highest to Lowest)#

PrecedenceOperatorDescription
1() []Parentheses, brackets
2! ~ ++ --Logical NOT, bitwise NOT, increment, decrement
3* / %Multiplication, division, modulus
4+ -Addition, subtraction
5<< >>Bitwise shifts
6< <= > >=Relational operators
7== !=Equality operators
8&Bitwise AND
9^Bitwise XOR
10|Bitwise OR
11&&Logical AND
12||Logical OR
13= += -= etc.Assignment operators

Example Without Parentheses#

int result = 2 + 3 * 4;  // Result: 14 (not 20)
// Because * has higher precedence than +
// Calculates as: 2 + (3 * 4)

Example With Parentheses#

int result1 = 2 + 3 * 4;   // Result: 14
int result2 = (2 + 3) * 4; // Result: 20

printf("%d\n", result1);   // 14
printf("%d\n", result2);   // 20

Complex Expression Example#

#include <stdio.h>

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

    // Without parentheses
    int result1 = a + b * c / 2;  // 10 + (5 * 2 / 2) = 15

    // With parentheses (clearer)
    int result2 = (a + b) * c / 2;  // (10 + 5) * 2 / 2 = 15

    printf("Result 1: %d\n", result1);
    printf("Result 2: %d\n", result2);

    return 0;
}

Type Casting in Expressions#

#include <stdio.h>

int main() {
    int x = 7;
    int y = 2;

    // Integer division (no decimal)
    printf("Integer: %d\n", x / y);        // 3

    // Cast to float
    printf("Float: %.2f\n", (float)x / y); // 3.50

    // Cast both
    printf("Float: %.2f\n", (float)x / (float)y); // 3.50

    return 0;
}

Practical Example: Grade Calculator#

#include <stdio.h>

int main() {
    int math, english, science;
    float average;

    printf("Enter marks: Math English Science\n");
    scanf("%d %d %d", &math, &english, &science);

    average = (math + english + science) / 3.0;

    printf("\nAverage: %.2f\n", average);

    // Using logical operators
    if (average >= 90 && average <= 100) {
        printf("Grade: A+\n");
    } else if (average >= 80 && average < 90) {
        printf("Grade: A\n");
    } else if (average >= 70 && average < 80) {
        printf("Grade: B\n");
    } else if (average >= 60 && average < 70) {
        printf("Grade: C\n");
    } else if (average >= 50 && average < 60) {
        printf("Grade: D\n");
    } else {
        printf("Grade: F\n");
    }

    return 0;
}

Practice Exercises#

Exercise 1: Arithmetic Operations#

Write a program that:

  • Takes 2 numbers as input
  • Performs all arithmetic operations
  • Displays results

Exercise 2: Comparison Program#

Write a program that:

  • Takes 3 numbers
  • Finds the largest using relational operators
  • Prints the result

Exercise 3: Eligibility Checker#

Write a program that checks if a person is eligible for a loan:

  • Age >= 21 AND
  • Salary >= 30000 AND
  • Credit Score >= 650

Summary#

✅ Learned all arithmetic operators
✅ Understood relational operators
✅ Mastered logical operators
✅ Explored bitwise operators
✅ Used assignment operators
✅ Understood operator precedence
✅ Performed type casting in expressions

Key Points to Remember#

  1. Always use == for comparison, not =
  2. Use parentheses to make complex expressions clear
  3. Logical operators use && (AND) and || (OR)
  4. Remember operator precedence to avoid bugs
  5. Pre and post increment/decrement have subtle differences

Next Steps#

Tomorrow we’ll use these operators in control statements - if/else, switch, and loops!

→ Continue to Day 5

← Back to Day 3