Operators

1. Operators

Operators in C are symbols that perform operations on variables and values. They are classified into several categories:

1.1. Arithmetic Operators

Used for basic arithmetic operations:

  • Addition: +

    int a = 5 + 3; // a = 8
  • Subtraction: -

    int a = 5 - 3; // a = 2
  • Multiplication: *

    int a = 5 * 3; // a = 15
  • Division: /

    int a = 6 / 3; // a = 2
  • Modulus (remainder): %

    int a = 5 % 3; // a = 2

1.2. Relational Operators

Used to compare values:

  • Equal to: ==

    if (a == b) // Checks if a is equal to b
  • Not equal to: !=

    if (a != b) // Checks if a is not equal to b
  • Greater than: >

    if (a > b) // Checks if a is greater than b
  • Less than: <

    if (a < b) // Checks if a is less than b
  • Greater than or equal to: >=

    if (a >= b) // Checks if a is greater than or equal to b
  • Less than or equal to: <=

    if (a <= b) // Checks if a is less than or equal to b

1.3. Logical Operators

Used to perform logical operations:

  • Logical AND: &&

  • Logical OR: ||

  • Logical NOT: !

1.4. Bitwise Operators

Operate on bits and perform bit-level operations:

  • Bitwise AND: &

  • Bitwise OR: |

  • Bitwise XOR: ^

  • Bitwise NOT: ~

  • Left Shift: <<

  • Right Shift: >>

1.5. Assignment Operators

Used to assign values to variables:

  • Simple Assignment: =

  • Add and Assign: +=

  • Subtract and Assign: -=

  • Multiply and Assign: *=

  • Divide and Assign: /=

  • Modulus and Assign: %=

1.6. Increment and Decrement Operators

Used to increase or decrease the value of a variable:

  • Increment: ++

  • Decrement: --

1.7. Conditional (Ternary) Operator

A shorthand for if-else statements:

1.8. Sizeof Operator

Used to get the size of a variable or type in bytes:

2. Instructions

Instructions in C are the basic commands that the compiler translates into machine code. Here are a few key instructions:

2.1. Assignment

2.2. Control Flow Instructions

  • if Statement

  • else Statement

  • switch Statement

  • while Loop

  • for Loop

  • do-while Loop

2.3. Function Calls

To call a function:

2.4. Return Statement

Returns a value from a function and exits the function:

2.5. Break and Continue

  • break: Exits the current loop or switch statement

  • continue: Skips the rest of the code in the current loop iteration and starts the next iteration


Summary

  • Operators in C include arithmetic, relational, logical, bitwise, assignment, increment/decrement, conditional, and sizeof operators.

  • Instructions include assignment, control flow statements (if, else, switch), loops (while, for, do-while), function calls, return, and loop control (break, continue)(We'll study more about them in Control Flow).

Last updated