Understanding C Language Operators

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations, including addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). Below is a sample program demonstrating the usage of these operators:

#include<stdio.h>

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

    printf("a + b = %d\n", a + b); // Output 13
    printf("a - b = %d\n", a - b); // Output 7
    printf("a * b = %d\n", a * b); // Output 30
    printf("a / b = %d\n", a / b); // Output 3 (integer division)
    printf("a %% b = %d\n", a % b); // Output 1 (modulus operation)

    return 0;
}

2. Relational Operators

Relational operators are used to compare the size relationship between two values, including greater than (>), less than (<), greater than or equal to (>=), less than or equal to (<=), equal to (==), and not equal to (!=). Below is a sample program:

#include<stdio.h>

int main() {
    int x = 5;
    int y = 8;

    printf("x > y: %d\n", x > y); // Output 0 (false)
    printf("x < y: %d\n", x < y); // Output 1 (true)
    printf("x >= y: %d\n", x >= y); // Output 0
    printf("x <= y: %d\n", x <= y); // Output 1
    printf("x == y: %d\n", x == y); // Output 0
    printf("x != y: %d\n", x != y); // Output 1

    return 0;
}

3. Logical Operators

Logical operators are used to combine multiple conditional expressions, including logical AND (&&), logical OR (||), and logical NOT (!). The sample program is as follows:

#include<stdio.h>

int main() {
    int p = 1;
    int q = 0;

    printf("p && q: %d\n", p && q); // Output 0 (false)
    printf("p || q: %d\n", p || q); // Output 1 (true)
    printf("!p: %d\n", !p); // Output 0 (since p is true, negation is false)

    return 0;
}

4. Assignment Operators

Assignment operators are used to assign values to variables, commonly including simple assignment (=) and compound assignment operators (such as +=, -=, *=, etc.). Example:

#include<stdio.h>

int main() {
    int num = 10;

    num += 5; // Equivalent to num = num + 5
    printf("num after += 5: %d\n", num); // Output 15

    num -= 3; // Equivalent to num = num - 3
    printf("num after -= 3: %d\n", num); // Output 12

    num *= 2; // Equivalent to num = num * 2
    printf("num after *= 2: %d\n", num); // Output 24

    num /= 4; // Equivalent to num = num / 4
    printf("num after /= 4: %d\n", num); // Output 6

    num %= 5; // Equivalent to num = num % 5
    printf("num after %%= 5: %d\n", num); // Output 1

    return 0;
}

5. Increment and Decrement Operators

Increment (++) and decrement (–) operators are used to increase or decrease the value of a variable by 1. They can be placed before (prefix) or after (postfix) the variable, which affects the value of the expression. Sample program:

#include<stdio.h>

int main() {
    int count = 5;

    printf("count++: %d\n", count++); // Output 5, then count becomes 6
    printf("++count: %d\n", ++count); // count becomes 7 first, then outputs 7

    int value = 10;
    printf("value--: %d\n", value--); // Output 10, then value becomes 9
    printf("--value: %d\n", --value); // value becomes 8 first, then outputs 8

    return 0;
}

6. Bitwise Operators

Bitwise operators are used to operate on the binary bits of integers, including bitwise AND (&), bitwise OR (|), bitwise XOR (^), bitwise NOT (~), left shift (<<), and right shift (>>). Example:

#include<stdio.h>

int main() {
    int x = 5; // Binary: 0101
    int y = 3; // Binary: 0011

    printf("x && y: %d\n", x && y); // 0001 → 1
    printf("x | y: %d\n", x | y); // 0111 → 7
    printf("x ^ y: %d\n", x ^ y); // 0110 → 6
    printf("~x: %d\n", ~x); // In a 32-bit system, ~5 is -6

    int z = 8; // Binary: 1000
    printf("z << 1: %d\n", z << 1); // 10000 → 16 (left shift by one is equivalent to multiplying by 2)
    printf("z >> 1: %d\n", z >> 1); // 100 → 4 (right shift by one is equivalent to dividing by 2)

    return 0;
}

7. Conditional Operator

The conditional operator (?:) is the only ternary operator in C language, used to select a value based on a condition. Example:

#include<stdio.h>

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

    int max = (a > b) ? a : b;
    printf("Max value is: %d\n", max); // Output 20

    return 0;
}

8. Comma Operator

The comma operator is used to combine multiple expressions into one expression, evaluating each expression in turn and returning the value of the last expression. Example:

#include<stdio.h>

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

    int result = (x++, y--, x + y);
    printf("Result: %d\n", result); // Output 14 (5 + 9)

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

    return 0;
}

9. Operator Precedence and Associativity

Operator precedence determines the order of operations in an expression. Operators with higher precedence are evaluated first. Associativity determines the direction of evaluation when operators have the same precedence (left to right or right to left). For example:

#include<stdio.h>

int main() {
    int a = 10 + 3 * 5; // First calculate multiplication (3*5=15), then addition → 25
    printf("a: %d\n", a);

    int b = 20 / 2 % 3; // Precedence: / and % are the same, associativity left to right → (10 % 3) = 1
    printf("b: %d\n", b);

    int c = 5 + 4 - 3 + 2; // Associativity left to right → ((5+4)-3)+2 = 8
    printf("c: %d\n", c);

    return 0;
}

The above is a detailed explanation and examples of C language operators. In the next lesson, we will continue to learn about the control structures in C language, including if statements, if-else statements, if-else if-else statements, and switch statements, which allow programs to execute different code paths based on different conditions, achieving more flexible program logic control.

Leave a Comment