○ Key Learning Points from Week 4
- Learned about the concepts and types of arithmetic operators, relational operators, logical operators, and conditional operators.
- Studied increment/decrement operators, compound assignment operators, comma operators, and bitwise operators.
- Understood type conversion and operator precedence.
○ Expression (Formula)
An expression is a combination of constants, variables, and operators, divided into operators and operands.
○ Arithmetic Operators (Basic Arithmetic): +, -, *, /, %
- Division between
inttypes results in anint(decimal parts are truncated). - Division between
floattypes yieldsfloatresults. -
%(Modulus Operator) returns the remainder of dividing the first operand by the second operand.
○ Increment/Decrement Operators
-
++variable,variable++,--variable,variable-- - The position of the increment/decrement operator affects when the value is updated.
○ Assignment Operators
Operators that assign the result of an expression to a variable.
- The left side must be a variable, while the right side can be any expression.
100 = x + y; // Error
x = x + 1; // Valid (Different from the math "="!)
y = x = 3; // Valid (Assigns 3 to x, then assigns x's value to y)
○ Compound Assignment Operators
Combines = with arithmetic operators, e.g., x += y.
- Allows shorthand for reassigning the result to the same variable.
○ Relational Operators: ==, !=, >, <, >=, <=
- Compares two operands; returns TRUE (
1) or FALSE (0). - Cannot chain comparisons as in math:
2 < x < 5→ This is invalid.- Correct way:
(2 < x) && (x < 5)
- Correct way:
○ Logical Operators: &&, ||, !
- Used to combine multiple conditions.
- Returns
1(TRUE) if the condition is met,0(FALSE) otherwise. - In C, non-zero values are considered TRUE and
0is FALSE. - Note: In this class, negative numbers were also treated as FALSE due to representing no electric signal.
○ Ternary Operator (Conditional Operator)
- The only operator that takes three operands:
max_value = (x > y) ? a : b; // Returns a if TRUE, b if FALSE
○ Comma Operator
- Evaluates two expressions separated by
,sequentially.
○ Bitwise Operators: &, |, ^, <<, >>, ~
-
<<shifts bits to the left, effectively doubling the value per shift (due to binary nature).
○ Type Conversion & Operator Precedence
Type Conversion (Casting)
- Changes the data type during program execution.
- Be cautious—improper casting can lead to data loss.
- When mixing different data types, C automatically promotes to the larger type.
Explicit Casting
- Developer explicitly converts the type using parentheses before the variable:
(int)1.35 // Casts 1.35 to int
Operator Precedence
- Determines the order of operations among multiple operators.
- Refer to the textbook’s precedence chart for details.
○ Practice
Try writing and executing C code using the operators learned this week.



Top comments (0)