Operators in C Language

 

1. Arithmetic Operators:

  • + (Addition): Adds two operands.

    c
    int a = 5, b = 3, result; result = a + b; // result = 8
  • - (Subtraction): Subtracts right operand from left operand.

    c
    int a = 5, b = 3, result; result = a - b; // result = 2
  • * (Multiplication): Multiplies two operands.

    c
    int a = 5, b = 3, result; result = a * b; // result = 15
  • / (Division): Divides left operand by right operand.

    c
    int a = 10, b = 2, result; result = a / b; // result = 5
  • % (Modulus): Returns the remainder after division of left operand by right operand.

    c
    int a = 10, b = 3, result; result = a % b; // result = 1

2. Relational Operators:

  • == (Equal to): Checks if two operands are equal.

    c
    int a = 5, b = 5; if (a == b) { // Code executes as a equals b }
  • != (Not equal to): Checks if two operands are not equal.

    c
    int a = 5, b = 3; if (a != b) { // Code executes as a is not equal to b }
  • > (Greater than): Checks if left operand is greater than right operand.

    c
    int a = 5, b = 3; if (a > b) { // Code executes as a is greater than b }
  • < (Less than): Checks if left operand is less than right operand.

    c
    int a = 3, b = 5; if (a < b) { // Code executes as a is less than b }
  • >= (Greater than or equal to): Checks if left operand is greater than or equal to right operand.

    c
    int a = 5, b = 5; if (a >= b) { // Code executes as a is greater than or equal to b }
  • <= (Less than or equal to): Checks if left operand is less than or equal to right operand.

    c
    int a = 3, b = 5; if (a <= b) { // Code executes as a is less than or equal to b }

3. Logical Operators:

  • && (Logical AND): Returns true if both operands are true.

    c
    int a = 1, b = 1; if (a && b) { // Code executes as both a and b are true }
  • || (Logical OR): Returns true if any one of the operands is true.

    c
    int a = 1, b = 0; if (a || b) { // Code executes as a is true }
  • ! (Logical NOT): Reverses the logical state of its operand.

    c
    int a = 0; if (!a) { // Code executes as a is false (0 is considered false) }

4. Assignment Operators:

  • = (Assignment): Assigns the value of the right expression to the left operand.

    c
    int a, b; a = 5; // 'a' is assigned the value 5 b = a; // 'b' is assigned the value of 'a', i.e., 5
  • += (Add and Assign): Adds the right operand to the left operand and assigns the result to the left operand.

    c
    int a = 5; a += 3; // 'a' is now 8 (5 + 3)
  • -= (Subtract and Assign): Subtracts the right operand from the left operand and assigns the result to the left operand.

    c
    int a = 5; a -= 3; // 'a' is now 2 (5 - 3)
  • *= (Multiply and Assign): Multiplies the right operand with the left operand and assigns the result to the left operand.

    c
    int a = 5; a *= 3; // 'a' is now 15 (5 * 3)
  • /= (Divide and Assign): Divides the left operand by the right operand and assigns the result to the left operand.

    c
    int a = 15; a /= 3; // 'a' is now 5 (15 / 3)
  • %= (Modulus and Assign): Takes modulus of the left operand with the right operand and assigns the result to the left operand.

    c
    int a = 17; a %= 5; // 'a' is now 2 (17 % 5)

5. Bitwise Operators:

  • & (Bitwise AND): Performs bitwise AND on the operands.

    c
    int a = 5, b = 3, result; result = a & b; // result = 1 (0101 & 0011 = 0001)
  • | (Bitwise OR): Performs bitwise OR on the operands.

    c
    int a = 5, b = 3, result; result = a | b; // result = 7 (0101 | 0011 = 0111)
  • ^ (Bitwise XOR): Performs bitwise XOR on the operands.

    c
    int a = 5, b = 3, result; result = a ^ b; // result = 6 (0101 ^ 0011 = 0110)
  • ~ (Bitwise NOT): Inverts the bits of its operand.

    c
    int a = 5, result; result = ~a; // result = -6 (in 2's complement form)
  • << (Left Shift): Shifts the bits of the left operand to the left by a specified number of positions.

    c
    int a = 5, result; result = a << 2; // result = 20 (0000 0101 << 2 = 0001 0100)
  • >> (Right Shift): Shifts the bits of the left operand to the right by a specified number of positions.

    c
    int a = 20, result; result = a >> 2; // result = 5 (0001 0100 >> 2 = 0000 0101)

6. Increment and Decrement Operators:

  • ++ (Increment): Increases the value of the variable by 1.

    c
    int a = 5; a++; // 'a' is now 6
  • -- (Decrement): Decreases the value of the variable by 1.

    c
    int a = 5; a--; // 'a' is now 4

7. Conditional (Ternary) Operator:

  • ? : (Conditional Operator): Evaluates an expression and returns a value based on whether the expression is true or false.
    c
    int a = 5, b = 3, max; max = (a > b) ? a : b; // max = 5 (since a > b is true)

8. Comma Operator:

  • , (Comma Operator): Evaluates both operands and returns the value of the right operand.
    c
    int a = 5, b = 3, result; result = (a++, b++); // result = 3 (value of b after increment)

Comments

Popular posts from this blog

Flowchart and Algorithm in C Language

Mastering the Fundamentals: A Comprehensive Guide to C Language