Constants, Variables, Type Casting & Operator Hierarchy
Unit 1: Algorithms, Flowcharts & C Program Architecture • Problem Solving Using C
👨🏫 Professor's Mental Model: Currency Exchange Counters & Mathematical Priority Rules
Variable ek aisi tijori hai jiska saman samay ke sath badla ja sakta hai. Constant ek stamped stone tablet hai jispe likha data hamesha ke liye freeze ho jata hai. Type Casting ek currency exchange booth jaisa hai: jaise Rupees ko Dollars me convert kiya jata hai, waise hi integer data ko float me badla jata hai. Aur Operators cricket umpire ke rules hain jo decide karte hain ki pehle kaunsa shot khela jayega (Precedence & Associativity)!
Academic Lecture Notes & Solved Study Pages
Unit 1 • Core Concepts, Step-by-Step Proofs & Notebook Solutions
1. Variables & Constants: Definition, Declaration & L-Value vs R-Value
Example: `x = 10;` is legal (x is L-value). `10 = x;` is a SYNTAX ERROR (10 is an R-value without a variable memory location).
1. Literal Constants: 100 (int), 3.14f (float), 'Z' (char), "Hello" (string).
2. const Keyword: `const float PI = 3.14159f;` (read-only variable, typed, checked by compiler).
3. Preprocessor Macro: `#define PI 3.14159` (text substitution, untyped).
2. Type Conversion: Implicit Promotion vs Explicit Casting
Converting an expression of one data type into another:
The compiler automatically promotes smaller types to larger types without data loss to prevent truncation (Widening Conversion):
`char -> short -> int -> unsigned int -> long -> float -> double -> long double`.
Example: `int a = 5; float b = 2.5; float c = a + b;` (a is promoted to 5.0f before addition).
Forced by the programmer using cast operator syntax `(target_type) expression`.
Example: In integer division `int a = 5, b = 2; float result = (float)a / b;` yields 2.50. Without `(float)`, `5 / 2` would truncate to 2.00!
3. The 8 Operator Categories in C
1. Arithmetic Operators: `+`, `-`, `*`, `/` (quotient), `%` (modulus remainder - integers only!).
2. Relational Operators: `==`, `!=`, `<`, `>`, `<=`, `>=` (return 1 for true, 0 for false).
3. Logical Operators: `&&` (Logical AND), `||` (Logical OR), `!` (Logical NOT) with Short-Circuit Evaluation.
4. Bitwise Operators: `&` (Bitwise AND), `|` (Bitwise OR), `^` (Bitwise XOR), `~` (Bitwise NOT), `<<` (Left Shift: multiply by 2^n), `>>` (Right Shift: divide by 2^n).
5. Assignment Operators: Simple `=`, Compound `+=`, `-=`, `*=`, `/=`, `%=`.
6. Increment / Decrement: Pre-increment `++x` (increment then use), Post-increment `x++` (use then increment).
7. Conditional / Ternary Operator: `(condition) ? expression1 : expression2`.
8. Special Operators: `sizeof`, `,` (comma operator), `&` (address-of), `*` (dereference).
| Priority (Rank) | Operator Description | Symbols | Associativity |
|---|---|---|---|
| 1 (Highest) | Postfix, Function Call, Array Subscript | () [] -> . x++ x-- | Left to Right |
| 2 | Unary Prefix, Logical NOT, Dereference, sizeof | ++x --x + - ! ~ * & (type) sizeof | Right to Left |
| 3 | Multiplicative | * / % | Left to Right |
| 4 | Additive | + - | Left to Right |
| 5 | Bitwise Shift | << >> | Left to Right |
| 6 | Relational Inequality | < <= > >= | Left to Right |
| 7 | Relational Equality | == != | Left to Right |
| 8 | Bitwise AND | & | Left to Right |
| 9 | Bitwise XOR | ^ | Left to Right |
| 10 | Bitwise OR | | | Left to Right |
| 11 | Logical AND | && | Left to Right |
| 12 | Logical OR | || | Left to Right |
| 13 | Conditional Ternary | ? : | Right to Left |
| 14 | Assignment Operators | = += -= *= /= %= <<= >>= | Right to Left |
| 15 (Lowest) | Comma Operator | , | Left to Right |
Interactive Tested Code Example
#include <stdio.h>
int main(void) {
int a = 10, b = 20, max_val;
// 1. Ternary Conditional Operator
max_val = (a > b) ? a : b;
printf("Largest of %d and %d is: %d\n", a, b, max_val);
// 2. Pre vs Post Increment Demonstration
int x = 5, y = 5;
int pre_res = ++x; // x becomes 6, pre_res gets 6
int post_res = y++; // post_res gets 5, then y becomes 6
printf("Pre-increment: x = %d, pre_res = %d\n", x, pre_res);
printf("Post-increment: y = %d, post_res = %d\n", y, post_res);
// 3. Bitwise Shift Trick (Multiplication & Division by 2^n)
int num = 12;
printf("%d << 2 (Multiply by 4) = %d\n", num, num << 2);
printf("%d >> 2 (Divide by 4) = %d\n", num, num >> 2);
return 0;
}🎯 University Exam Scoring Blueprint
- The Modulus operator (%) strictly operates only on integer operands. Writing 7.5 % 2 results in a compile error.
- Explain Short-Circuit Evaluation: in (A && B), if A is false, B is NEVER evaluated. In (A || B), if A is true, B is NEVER evaluated.
- Memorize that Assignment (=) and Ternary (? :) evaluate Right to Left, whereas Arithmetic operators evaluate Left to Right.