C ProgrammingUnit 39 min read
Operators & Expressions: Arithmetic, Relational, Logical, Bitwise, Assignment & Special
Unit 3 of C Programming: Explores how operators manipulate data, evaluate expressions, and control logic—from basic arithmetic to bitwise tricks—with step-by-step examples, precedence rules, and real-world ties to apps like eSewa’s transaction checks and Daraz’s discount calculations.
TAKEAWAYS
- Operators are symbols that perform operations on variables/values (e.g.,
+,==,&&). - Precedence and associativity dictate evaluation order (e.g.,
*before+). - Relational (
>,<=) and logical (&&,||) operators enable decision-making. - Bitwise operators (
<<,&) manipulate binary data (used in compression, encryption). - Assignment (
=,+=) and comma operators chain operations. - Type casting ensures correct data conversion (e.g.,
(int)3.7).
1. Introduction to Operators
Operators are symbols that perform operations on operands (variables/values). They are categorized into arithmetic, relational, logical, bitwise, assignment, and special operators.
1.1 Types of Operators
| Category | Operators | Example | Description |
|---|---|---|---|
| Arithmetic | +, -, *, /, % |
a + b |
Basic math operations. |
| Relational | ==, !=, >, <, >=, <= |
x > y |
Compare values (returns 1/0). |
| Logical | &&, ` |
, !` |
|
| Bitwise | &, ` |
, ^, ~`, <<, >> |
x & y |
| Assignment | =, +=, -=, *=, /=, %= |
a += b |
Assign values (shorthand forms). |
| Special | ,, ?:, sizeof, _ (scope) |
a, b |
Chaining, ternary, type size, scope. |
2. Arithmetic Operators
Perform mathematical operations.
2.1 Operators and Examples
flowchart TD
A["Arithmetic Operators"] --> B["+" Addition]
A --> C["-" Subtraction]
A --> D["*"] Multiplication
A --> E["/"] Division
A --> F["%" Modulus]
A --> G["++" Increment]
A --> H["--" Decrement]Example:
int a = 10, b = 3;
printf("%d\n", a / b); // Output: 3 (integer division)
printf("%d\n", a % b); // Output: 1 (remainder)
2.2 Precedence and Associativity
- Precedence (highest to lowest):
%,*,/,+,-. - Associativity: Left-to-right for same precedence.
Example:
int x = 10 + 2 * 3; // 2*3 first → 16
3. Relational Operators
Compare two values and return 1 (true) or 0 (false).
3.1 Operators and Truth Tables
| Operator | Name | Example (x=5, y=3) |
Result (x vs y) |
|---|---|---|---|
== |
Equal | x == y |
0 (false) |
!= |
Not equal | x != y |
1 (true) |
> |
Greater | x > y |
1 (true) |
< |
Less | x < y |
0 (false) |
>= |
Greater/= | x >= y |
1 (true) |
<= |
Less/= | x <= y |
0 (false) |
Example:
int a = 7, b = 5;
printf("%d\n", a > b); // Output: 1 (true)
4. Logical Operators
Combine relational expressions.
4.1 Operators and Truth Tables
| Operator | Name | Example (A=1, B=0) |
Result (A AND B) |
|---|---|---|---|
&& |
AND | A && B |
0 (false) |
| ` | ` | OR | |
! |
NOT | !A |
0 (false) |
Example:
int x = 4, y = 5;
printf("%d\n", (x > 2) && (y < 10)); // Output: 1 (true)
5. Bitwise Operators
Manipulate binary data (used in low-level programming).
5.1 Operators and Examples
flowchart TD
A["Bitwise Operators"] --> B["&"] Bitwise AND
A --> C["|"] Bitwise OR
A --> D["^"] Bitwise XOR
A --> E["~"] Bitwise NOT
A --> F["<<"] Left Shift
A --> G[">>"] Right Shift
A --> H["~="] Bitwise NOT Equal
A --> I["&="] Bitwise AND Assignment
A --> J["|="] Bitwise OR Assignment
A --> K["^="] Bitwise XOR AssignmentExample (Bitwise AND):
int a = 5 (0101), b = 3 (0011);
printf("%d\n", a & b); // Output: 1 (0001)
Real-World Use:
- eSewa’s Transaction Checks: Bitwise operations verify checksums in digital payments.
- Compression Algorithms: XOR (
^) is used in data encryption (e.g., AES).
6. Assignment Operators
Assign values with shorthand.
| Operator | Example | Equivalent To |
|---|---|---|
= |
a = b |
a = b |
+= |
a += b |
a = a + b |
-= |
a -= b |
a = a - b |
*= |
a *= b |
a = a * b |
/= |
a /= b |
a = a / b |
%= |
a %= b |
a = a % b |
Example:
int x = 5;
x += 3; // x becomes 8
7. Special Operators
7.1 Comma Operator (,**)
Evaluates expressions left-to-right and returns the last value.
Example:
int a = (x = 2, y = 3, x + y);
printf("%d\n", a); // Output: 5 (last evaluated)
7.2 Ternary Operator (?:)
Conditional expression: condition ? expr1 : expr2.
Example:
int max = (a > b) ? a : b;
7.3 sizeof Operator
Returns size (in bytes) of a variable/type.
Example:
printf("%d\n", sizeof(int)); // Output: 4 (on most systems)
8. Operator Precedence Table
| Category | Operators | Precedence |
|---|---|---|
| Highest | (), [], ->, sizeof |
1 |
!, ~, ++, --, (type) |
2 | |
*, /, % |
3 | |
+, - |
4 | |
<<, >> |
5 | |
<, <=, >, >= |
6 | |
==, != |
7 | |
& |
8 | |
^ |
9 | |
| ` | ` | |
&& |
11 | |
| ` | ||
| Lowest | ?:, =, +=, -=, etc. |
13 |
9. Type Casting
Convert one data type to another.
Example:
float f = 3.14;
int i = (int)f; // i becomes 3 (truncated)
Real-World Use:
- Daraz Discounts: Converting
floatprices tointcents for calculations. - Bank Loans: Converting interest rates (
double) tointfor monthly payments.
10. Worked Example: eSewa Transaction Validation
Scenario: Verify if a transaction amount is within eSewa’s limits (₹10–₹10,000).
#include <stdio.h>
int main() {
float amount = 500.50f;
int valid = (amount >= 10) && (amount <= 10000);
printf("Transaction %s\n", valid ? "valid" : "invalid");
return 0;
}
Output:
Transaction valid
11. In the Real World
eSewa/Khalti
- Logical Operators (
&&) check if both payment details and amount are valid before processing. - Bitwise Operators (
^) encrypt transaction IDs for security.
- Logical Operators (
Daraz Order Processing
- Relational Operators (
>,<) prioritize orders based on urgency (e.g.,if (order_time < 10) { rush_delivery = true; }).
- Relational Operators (
NTC/Ncell Signal Strength
- Bitwise Shifts (
<<) adjust signal thresholds in mobile networks (e.g.,threshold = 3 << 2sets a 12-unit limit).
- Bitwise Shifts (
12. Exam Tip
- Always check precedence when combining operators (e.g.,
a = b + c * devaluatesc*dfirst). - Logical operators (
&&,||) return1/0, nottrue/false(unlike Java). - Bitwise tricks are rarely needed for basic exams but appear in low-level coding questions.
- Practice type casting—exams often test implicit vs. explicit conversions.
- Memorize precedence table—it’s the #1 question in past exams.
Key Formula:
For expressions like a = b + c * d, always parenthesize to avoid errors:
a = (b + c) * d; // Different from b + (c * d)
Based on the TU BITM syllabus for C Programming (IT232), unit 3.
Discussion
Loading…