Lecture Notes on Operators in C
Introduction
- Previous topic: Character data type
- Current topic: Operators in C
- Focus: Detailed discussion on every type of operator with examples and programs.
What is an Operator?
- Definition: An operator is a symbol that tells the compiler/computer which operation to perform on data.
- Purpose: Used for mathematical or logical manipulation of variables or data.
- Examples of Operators:
- Arithmetic Operators:
+, -, *, /
- Logical Operators:
&&, ||
- Relational Operators:
>, <, ==, !=*
Classification of Operators
Operators can be classified based on:
- Operations
- Operands
Overview of Operators
- Operators are used to manipulate variables and constants.
- Expression: A sequence of operators and operands that yields a single value after processing.
- Example:
a = 5 + 5 results in a = 10.
Types of Operators Based on Operands
-
Unary Operators:
- Definition: Operate on a single operand.
- Examples:
- Unary Minus (
-): Changes sign of the operand.
- Increment (
++), Decrement (--), Logical Not (!), Address of (&), Size of (sizeof).
- Example of Unary Minus:
- If
a = 5, then -a results in -5.
-
Binary Operators:
- Definition: Operate on two operands.
- Examples:
- Arithmetic:
+, -, *, /
- Relational:
>, <, ==, !=
- Logical:
&&, ||
- Bitwise:
&, |, ^, <<, >>
- Example:
a + b where a and b are two operands.
-
Ternary Operator:
- Definition: Requires three operands, also known as the conditional operator.
- Syntax:
condition ? expression1 : expression2
- Example:
- If
a = 10 and b = 15, then x = (a > b) ? a : b results in x = 15.*
Detailed Examples of Operators
Unary Operators
- Increment and Decrement:
- Prefix:
++x or --x (value changes before usage).
- Postfix:
x++ or x-- (value changes after usage).
- Example of Postfix:
- If
x = 10, then y = x++ results in y = 10 and x = 11.
- Example of Prefix:
- If
x = 10, then y = ++x results in y = 11 and x = 11.
Logical Not Operator
- Converts true to false and vice versa.
- Example:
- If
x = 11 and y = 10, then !(x > y) results in false.
Address of Operator
- Fetches the address of a variable in memory.
- Commonly used with the
scanf function and pointers.
Sizeof Operator
- Returns the size of a data type in bytes.
- Example:
sizeof(int) might return 4 bytes.
Conclusion
- Next video will focus on types of operators based on operations.
- Call to action: Request for a separate video on the ternary operator for detailed discussion.