C Operators

🧮 C Operators – Perform Operations in C

Operators in C are special symbols used to perform operations on variables and values. They are the building blocks for performing calculations, comparisons, and logical decisions in your programs.

🔹 Types of C Operators

  • Arithmetic Operators: +, -, *, /, %
  • Assignment Operators: =, +=, -=, etc.
  • Comparison (Relational) Operators: ==, !=, >, <, >=, <=
  • Logical Operators: &&, ||, !
  • Increment/Decrement: ++, --
  • Bitwise Operators: &, |, ^, ~, <<, >>

📝 Example: Arithmetic Operators

This example shows how to use basic arithmetic operations in C.

#include <stdio.h>

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

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

    return 0;
}
  

Try It Now

📝 Example: Comparison Operators

Compare two numbers and print the result using comparison operators.

#include <stdio.h>

int main() {
    int x = 5, y = 10;

    printf("x == y: %d\n", x == y);
    printf("x != y: %d\n", x != y);
    printf("x > y: %d\n", x > y);
    printf("x <= y: %d\n", x <= y);

    return 0;
}
  

Try It Now

📝 Example: Logical Operators

Use logical AND and OR to test conditions.

#include <stdio.h>

int main() {
    int age = 20;
    int hasID = 1;

    if (age >= 18 && hasID) {
        printf("Access granted.\n");
    } else {
        printf("Access denied.\n");
    }

    return 0;
}
  

Try It Now

🎯 Pro Tips

  • Operator precedence determines the order in which operations are evaluated.
  • Use parentheses () to control evaluation when in doubt.
  • Don't confuse = (assignment) with == (comparison)!

🧪 Practice Task

Try combining arithmetic and logical operators in a program. For example, check if the sum of two numbers is greater than 10.