C Operators Notes - Custom for Boss
Chapter 1: Introduction to Operators in C
Operators are symbols that perform operations on variables and values. They are categorized into
several types, including arithmetic, relational, logical, bitwise, assignment, and ternary operators.
Chapter 2: 1. Arithmetic Operators
Used to perform basic arithmetic calculations like addition, subtraction, multiplication, division, and
modulus.
int a = 10, b = 3;
int sum = a + b; // sum = 13
int diff = a - b; // diff = 7
int product = a * b; // product = 30
int quotient = a / b; // quotient = 3
int remainder = a % b; // remainder = 1
Chapter 3: 2. Relational Operators
Used to compare two values and return 1 (true) or 0 (false). Examples: ==, !=, >, <, >=, <=.
int a = 10, b = 20;
int result = (a == b); // result = 0
int greater = (a > b); // greater = 0
int less_eq = (a <= b); // less_eq = 1
Chapter 4: 3. Logical Operators
Used to combine multiple conditions. Operators include AND (&&), OR (||), NOT (!).
int a = 10, b = 5;
int cond1 = (a > 0 && b > 0); // 1 if both true
int cond2 = (a > 0 || b < 0); // 1 if at least one true
int cond3 = !(a > 0); // 0 because a > 0
C Operators Notes - Custom for Boss
Chapter 5: 4. Assignment Operators
Used to assign and update values to variables. Examples include =, +=, -=, *=, /=.
int a = 5;
a += 3; // a = 8
a -= 2; // a = 6
a *= 4; // a = 24
a /= 6; // a = 4
Chapter 6: 5. Increment and Decrement Operators
Used to increase or decrease a value by 1. Pre-increment (++a) and post-increment (a++) differ in
order of operation.
int a = 5;
int b = ++a; // a=6, b=6 (increment before assignment)
int c = a++; // c=6, a=7 (increment after assignment)
Chapter 7: 6. Bitwise Operators
Operate on bits of numbers. Examples include &, |, ^, ~, <<, >>.
int a = 5; // 0101 in binary
int b = 3; // 0011 in binary
int c = a & b; // 0001 (1)
int d = a | b; // 0111 (7)
int e = a ^ b; // 0110 (6)
int f = ~a; // bitwise NOT
int g = a << 1; // left shift (10)
int h = a >> 1; // right shift (2)
Chapter 8: 7. Ternary Operator
A shorthand for if-else that returns one of two values based on a condition: condition ? expr1 :
C Operators Notes - Custom for Boss
expr2.
int a = 10, b = 20;
int max = (a > b) ? a : b;
Chapter 9: Tips for Mastering Operators
- Use parentheses to avoid confusion with operator precedence.
- Test increment/decrement operators separately to understand their effects.
- Bitwise operators can optimize low-level tasks like permission checks.
- Comment your code for clarity.
Chapter 10: Practice Questions
Try solving these operator problems:
1. Arithmetic operations on two numbers.
2. Compare two numbers using relational operators.
3. Check conditions with logical operators.
4. Update variable with assignment operators.
5. Demonstrate pre and post increment/decrement.
6. Perform bitwise operations on two integers.
7. Find max of two numbers using ternary operator.
8. Check even or odd using modulus.
9. Swap two numbers without a third variable.
10. Evaluate combined operator expressions.