Notes on Operators in C Programming

Jul 27, 2024

Lecture Notes: Motivation and Introduction to Operators in C

Introduction to Operators

  • Operators are essential for performing calculations and comparisons in programming.
  • Examples of practical problems discussed to illustrate operator use:
    • Calculating area of a rectangular garden.
    • Comparing company turnovers.
    • Making decisions based on conditions (e.g., having both chair and table).

Example Programs Discussed

  1. Finding Area of a Rectangle

    • Formula: Area = Length × Breadth
    • Given: Length = 15m, Breadth = 10m
    • Importance of the multiplication operator (arithmetic operator).
  2. Comparing Company Turnovers

    • Task: Compare annual turnover of two companies.
    • Given: Your company’s turnover = 300,000,000; Competitor’s turnover = 500,000,000.
    • Operator used: Greater Than or Equal To (relational operator).
  3. Decision Making based on Conditions

    • Scenario: Check if both chair and table are available before studying.
    • Operator used: Logical AND (&&) to evaluate both conditions.

Importance of Operators

  • Operators are fundamental to performing calculations and comparisons in programming:
    • Without operators, essential functions like division, multiplication, and comparisons would not exist.
    • They form the foundation of programming languages, including C.

Types of Operators in C

  1. Arithmetic Operators

    • Operators: +, -, *, /, %
  2. Increment and Decrement Operators

    • Notation: ++ (Increment), -- (Decrement)
  3. Relational Operators

    • Operators: == (Equal To), != (Not Equal To), <= (Less Than Equal To), >= (Greater Than Equal To), < (Less Than), > (Greater Than)
  4. Logical Operators

    • Operators: && (AND), || (OR), ! (NOT)
  5. Bitwise Operators

    • Operators: Bitwise AND, Bitwise OR, Bitwise XOR, Bitwise NOT, Right Shift, Left Shift
  6. Assignment Operators

    • Operators: =, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=
  7. Conditional Operator

    • Operators: Ternary operator (not explicitly detailed)
  8. Other Operators

    • Address of operator (&)
    • Pointer operator (*)
    • Sizeof operator
    • Comma operator

Conclusion

  • Operators are crucial for effective programming in C.
  • Understanding different types of operators aids in writing efficient and functioning code.
  • This overview sets the foundation for further detailed discussions on specific operators and their applications.

Thank you for attending the lecture!