Loading...

Operators in C

Operators in C are symbols that perform operations on variables and values. They are categorized into different types based on their functionality.

Types of Operators

1. Arithmetic Operators

Used to perform mathematical operations:

  • + (Addition)
  • - (Subtraction)
  • * (Multiplication)
  • / (Division)
  • % (Modulus - returns remainder)
int a = 10, b = 5;
printf("Sum: %d", a + b);

2. Relational Operators

Used to compare values:

  • == (Equal to)
  • != (Not equal to)
  • > (Greater than)
  • < (Less than)
  • >= (Greater than or equal to)
  • <= (Less than or equal to)
if (a > b) {
    printf("a is greater than b");
}

3. Logical Operators

Used for logical operations:

  • && (Logical AND)
  • || (Logical OR)
  • ! (Logical NOT)
if (a > 5 && b < 10) {
    printf("Both conditions are true");
}

4. Bitwise Operators

Operate at the bit level:

  • & (Bitwise AND)
  • | (Bitwise OR)
  • ^ (Bitwise XOR)
  • ~ (Bitwise Complement)
  • << (Left Shift)
  • >> (Right Shift)

5. Assignment Operators

Used to assign values:

  • = (Assigns a value)
  • += (Adds then assigns)
  • -= (Subtracts then assigns)
  • *= (Multiplies then assigns)
  • /= (Divides then assigns)
  • %= (Modulus then assigns)

6. Increment and Decrement Operators

Used to increase or decrease values:

  • ++ (Increment)
  • -- (Decrement)
int x = 5;
x++;
printf("%d", x); // Outputs 6

Example Program

#include <stdio.h>

int main() {
    int a = 10, b = 5;
    printf("Addition: %d\n", a + b);
    printf("Greater: %d\n", a > b);
    printf("Logical AND: %d\n", (a > 5 && b < 10));
    return 0;
}

If you have any questions, feel free to ask. Thank you for reading!

Thankyou!