What handles operations in C?

Here's a comprehensive list of operators in C, along with sample code and explanations:

1. Arithmetic Operators:

  • Addition +

  • Subtraction -

  • Multiplication *

  • Division /

  • Modulus %

#include <stdio.h>

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

    // Addition
    result = a + b;
    printf("Addition: %d\n", result);

    // Subtraction
    result = a - b;
    printf("Subtraction: %d\n", result);

    // Multiplication
    result = a * b;
    printf("Multiplication: %d\n", result);

    // Division
    result = a / b;
    printf("Division: %d\n", result);

    // Modulus
    result = a % b;
    printf("Modulus: %d\n", result);

    return 0;
}

2. Relational Operators:

  • Equal to ==

  • Not equal to !=

  • Greater than >

  • Less than <

  • Greater than or equal to >=

  • Less than or equal to <=

#include <stdio.h>

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

    // Equal to
    if (a == b)
        printf("Equal\n");
    else
        printf("Not Equal\n");

    // Not equal to
    if (a != b)
        printf("Not Equal\n");
    else
        printf("Equal\n");

    // Greater than
    if (a > b)
        printf("a is Greater\n");
    else
        printf("b is Greater\n");

    // Less than
    if (a < b)
        printf("a is Less\n");
    else
        printf("b is Less\n");

    // Greater than or equal to
    if (a >= b)
        printf("a is Greater or Equal\n");
    else
        printf("b is Greater\n");

    // Less than or equal to
    if (a <= b)
        printf("a is Less or Equal\n");
    else
        printf("b is Less\n");

    return 0;
}

3. Logical Operators:

  • Logical AND &&

  • Logical OR ||

  • Logical NOT !

#include <stdio.h>

int main() {
    int a = 1, b = 0;

    // Logical AND
    if (a && b)
        printf("Both are True\n");
    else
        printf("At least one is False\n");

    // Logical OR
    if (a || b)
        printf("At least one is True\n");
    else
        printf("Both are False\n");

    // Logical NOT
    if (!a)
        printf("a is False\n");
    else
        printf("a is True\n");

    return 0;
}

4. Assignment Operators:

  • Assignment =

  • Add and assign +=

  • Subtract and assign -=

  • Multiply and assign *=

  • Divide and assign /=

  • Modulus and assign %=

#include <stdio.h>

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

    // Assignment
    int c = a;

    // Add and assign
    c += b;  // equivalent to c = c + b;

    // Subtract and assign
    c -= b;  // equivalent to c = c - b;

    // Multiply and assign
    c *= b;  // equivalent to c = c * b;

    // Divide and assign
    c /= b;  // equivalent to c = c / b;

    // Modulus and assign
    c %= b;  // equivalent to c = c % b;

    return 0;
}

5. Increment and Decrement Operators:

  • Increment ++

  • Decrement --

#include <stdio.h>

int main() {
    int a = 5;

    // Increment
    a++;  // equivalent to a = a + 1;

    // Decrement
    a--;  // equivalent to a = a - 1;

    return 0;
}

6. Bitwise Operators:

  • Bitwise AND &

  • Bitwise OR |

  • Bitwise XOR ^

  • Bitwise NOT ~

  • Left shift <<

  • Right shift >>

#include <stdio.h>

int main() {
    int a = 5, b = 3, result;

    // Bitwise AND
    result = a & b;
    printf("Bitwise AND: %d\n", result);

    // Bitwise OR
    result = a | b;
    printf("Bitwise OR: %d\n", result);

    // Bitwise XOR
    result = a ^ b;
    printf("Bitwise XOR: %d\n", result);

    // Bitwise NOT
    result = ~a;
    printf("Bitwise NOT: %d\n", result);

    // Left shift
    result = a << 1;  // equivalent to a * 2^1
    printf("Left shift: %d\n", result);

    // Right shift
    result = a >> 1;  // equivalent to a / 2^1
    printf("Right shift: %d\n", result);

    return 0;
}

7. Conditional Operator (Ternary Operator):

  • ? :
#include <stdio.h>

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

    // Conditional Operator
    max = (a > b) ? a : b;

    printf("Maximum: %d\n", max);

    return 0;
}

8. sizeof Operator:

  • sizeof
#include <stdio.h>

int main() {
    int a;
    float b;
    char c;

    printf("Size of int: %lu bytes\n", sizeof(a));
    printf("Size of float: %lu bytes\n", sizeof(b));
    printf("Size of char: %lu bytes\n", sizeof(c));

    return 0;
}

9. Comma Operator:

  • ,
#include <stdio.h>

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

    // Comma Operator
    c = (a++, b++, a + b);

    printf("Result: %d\n", c);

    return 0;
}

These are the basic operators in C. Understanding and mastering these operators is crucial for writing efficient and effective C programs.