10 - C Operators - Mathematical and Logical Operations

Learn about arithmetic, comparison, and logical operators in C


What is this topic?

This guide explains C Operators - Mathematical and Logical Operations in simple terms, what it does, and how to use it in real C programs.

Why We Need It

  • It helps you write correct and reliable C code.
  • It makes your programs easier to read and maintain.
  • It is used in real projects and interviews.
  • It reduces common beginner mistakes.
  • It builds a strong foundation for advanced topics.

Use Cases

  • Building practical C programs step by step.
  • Solving real coding tasks with clean logic.
  • Preparing for exams, interviews, and projects.
  • Understanding and improving existing C code.

What Are Operators?

Operators are symbols that perform operations on values. Think of them like mathematical symbols: +, -, *, /.


Arithmetic Operators

Used for mathematical calculations.

Operator Name Example Result
+ Addition 5 + 3 8
- Subtraction 5 - 3 2
* Multiplication 5 * 3 15
/ Division 6 / 2 3
% Modulus (remainder) 7 % 3 1

Examples

#include <stdio.h>

int main() {
    int x = 10;
    int y = 3;
    
    printf("%d + %d = %d\n", x, y, x + y);      // 13
    printf("%d - %d = %d\n", x, y, x - y);      // 7
    printf("%d * %d = %d\n", x, y, x * y);      // 30
    printf("%d / %d = %d\n", x, y, x / y);      // 3
    printf("%d %% %d = %d\n", x, y, x % y);     // 1
    
    return 0;
}

Output:

10 + 3 = 13
10 - 3 = 7
10 * 3 = 30
10 / 3 = 3
10 % 3 = 1

Comparison Operators

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

Operator Name Example Result
== Equal to 5 == 5 1 (true)
!= Not equal to 5 != 3 1 (true)
> Greater than 5 > 3 1 (true)
< Less than 5 < 3 0 (false)
>= Greater or equal 5 >= 5 1 (true)
<= Less or equal 5 <= 3 0 (false)

Examples

#include <stdio.h>

int main() {
    int age = 18;
    
    printf("age == 18: %d\n", age == 18);      // 1 (true)
    printf("age != 18: %d\n", age != 18);      // 0 (false)
    printf("age > 17: %d\n", age > 17);        // 1 (true)
    printf("age < 21: %d\n", age < 21);        // 1 (true)
    printf("age >= 18: %d\n", age >= 18);      // 1 (true)
    printf("age <= 18: %d\n", age <= 18);      // 1 (true)
    
    return 0;
}

Logical Operators

Combine multiple conditions.

Operator Name Meaning
&& AND Both conditions true
|| OR At least one true
! NOT Opposite result

AND (&&)

int age = 25;
int income = 50000;

if (age > 18 && income > 40000) {
    printf("Eligible for loan\n");  // Both true, so this runs
}

OR (||)

int day = 6;  // Saturday

if (day == 6 || day == 7) {
    printf("It's the weekend!\n");  // At least one true
}

NOT (!)

int isRaining = 0;  // 0 means false

if (!isRaining) {
    printf("Let's go outside!\n");  // Opposite of 0 is 1 (true)
}

Assignment Operators

Assign values to variables.

Operator Example Equivalent
= x = 5 Assign 5 to x
+= x += 5 x = x + 5
-= x -= 5 x = x - 5
*= x *= 5 x = x * 5
/= x /= 5 x = x / 5
%= x %= 5 x = x % 5

Examples

int x = 10;

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

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

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

Increment and Decrement

Operator Meaning Example
++ Increment by 1 x++ or ++x
-- Decrement by 1 x-- or --x

Examples

int count = 5;

count++;
printf("%d\n", count);  // 6

count--;
printf("%d\n", count);  // 5

Practical Example

#include <stdio.h>

int main() {
    // Shopping calculation
    float itemPrice = 29.99;
    int quantity = 3;
    const float TAX = 0.08;
    
    float subtotal = itemPrice * quantity;
    float tax = subtotal * TAX;
    float total = subtotal + tax;
    
    printf("Item Price: $%.2f\n", itemPrice);
    printf("Quantity: %d\n", quantity);
    printf("Subtotal: $%.2f\n", subtotal);
    printf("Tax (8%%): $%.2f\n", tax);
    printf("Total: $%.2f\n", total);
    
    // Check if discount applies
    int discount = (total >= 100) ? 10 : 0;
    printf("Discount: $%d\n", discount);
    
    return 0;
}

Operator Precedence

Order of operations matters:

int result = 5 + 3 * 2;  // 11, not 16
// Multiplication happens first: 3 * 2 = 6
// Then addition: 5 + 6 = 11

Use parentheses to be clear:

int result1 = 5 + 3 * 2;   // 11
int result2 = (5 + 3) * 2; // 16

Quick Reference

// Arithmetic
int sum = 10 + 5;        // 15
int product = 10 * 5;    // 50

// Comparison
int equal = (10 == 10);  // 1 (true)
int greater = (10 > 5);  // 1 (true)

// Logical
int both = (1 && 1);     // 1 (true)
int either = (0 || 1);   // 1 (true)
int opposite = !0;       // 1 (true)

// Assignment
int x = 5;
x += 3;                  // x is now 8
x++;                     // x is now 9

Practice Exercise

Create operators.c:

#include <stdio.h>

int main() {
    int num1 = 20;
    int num2 = 8;
    
    printf("=== Arithmetic ===\n");
    printf("%d + %d = %d\n", num1, num2, num1 + num2);
    printf("%d - %d = %d\n", num1, num2, num1 - num2);
    printf("%d * %d = %d\n", num1, num2, num1 * num2);
    printf("%d / %d = %d\n", num1, num2, num1 / num2);
    printf("%d %% %d = %d\n", num1, num2, num1 % num2);
    
    printf("\n=== Comparison ===\n");
    printf("%d > %d: %d\n", num1, num2, num1 > num2);
    printf("%d < %d: %d\n", num1, num2, num1 < num2);
    printf("%d == %d: %d\n", num1, num2, num1 == num2);
    
    return 0;
}

:books: Navigation


:red_question_mark: Have Questions?

Comment below and we’ll help you! Need help with operators? Ask in the comments!