NBC 103 • Unit 116 min readHigh Exam Frequency

Constants, Variables, Type Casting & Operator Hierarchy

Unit 1: Algorithms, Flowcharts & C Program ArchitectureProblem 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

3 Notebook Pages
NOTEBOOK PAGE 1 OF 3

1. Variables & Constants: Definition, Declaration & L-Value vs R-Value

Variable: A named memory cell capable of storing mutating values during execution. Declaration reserves memory; initialization assigns an initial value.
L-Value vs R-Value: An L-value (Locator value) refers to an accessible memory address (appears on left of assignment '='). An R-value refers to a pure data value stored in an address.

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).

Constants in C:

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).

NOTEBOOK PAGE 2 OF 3

2. Type Conversion: Implicit Promotion vs Explicit Casting

Converting an expression of one data type into another:

Implicit Type Conversion (Automatic Type Promotion):

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).

Explicit Type Casting (Manual Type Conversion):

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!

NOTEBOOK PAGE 3 OF 3

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).

Master C Operator Precedence & Associativity Hierarchy
Priority (Rank)Operator DescriptionSymbolsAssociativity
1 (Highest)Postfix, Function Call, Array Subscript() [] -> . x++ x--Left to Right
2Unary Prefix, Logical NOT, Dereference, sizeof++x --x + - ! ~ * & (type) sizeofRight to Left
3Multiplicative* / %Left to Right
4Additive+ -Left to Right
5Bitwise Shift<< >>Left to Right
6Relational Inequality< <= > >=Left to Right
7Relational Equality== !=Left to Right
8Bitwise AND&Left to Right
9Bitwise XOR^Left to Right
10Bitwise OR|Left to Right
11Logical AND&&Left to Right
12Logical OR||Left to Right
13Conditional Ternary? :Right to Left
14Assignment 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;
}
💡 Note:Demonstrates ternary decision making, critical differences between prefix (++x) and postfix (x++) evaluation, and high-speed bitwise shifting.

🎯 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.

Top Viva Questions on Constants, Variables, Type Casting & Operator Hierarchy

2 Questions
1

What is the output of printf("%d", 5 / 2) vs printf("%f", 5.0 / 2)?

2

What is the difference between #define PI 3.14 and const float PI = 3.14?