NBC 103 • Unit 412 min readHigh Exam Frequency

Structures (struct), Unions, typedef & 4 Storage Classes

Unit 4: Structures, Unions, Enums & Storage ClassesProblem Solving Using C

👨‍🏫 Professor's Mental Model: The Student Identity Card vs Hotel Single Room

Structure ek Student ID card hai jisme Roll No (int), Name (string), aur Marks (float) sab ek saath rehte hain. Union ek shared hotel room hai jisme ek waqt me sirf ek hi mehmaan reh sakta hai — nayi value dalte hi purani overwrite ho jati hai!

Structures (struct) in C

User-defined data type grouping heterogeneous data items under a single name. Access members using dot operator (.) for values or arrow operator (->) for pointers to structures.

Structures vs Unions Comparison

Structure size = Sum of all member sizes (+ padding bytes). Union size = Size of the single LARGEST member. Structures store all members simultaneously; Unions share memory and store only one active member at a time.

The 4 Storage Classes in C

1. auto: Local stack memory, garbage default, local block scope, exits with block. 2. register: CPU register, garbage default, fast access (no & operator allowed). 3. static: Data segment, initialized to 0, retains value across multiple function calls! 4. extern: Global data segment, accessible across multiple C source files.

Interactive Tested Code Example

#include <stdio.h>

struct Student {
    int roll_no;
    char name[30];
    float marks;
};

int main(void) {
    struct Student s1 = {101, "Rahul Sharma", 92.5};
    struct Student *ptr = &s1;
    
    printf("Student Details via Dot Operator:\n");
    printf("Roll: %d | Name: %s | Marks: %.1f\n\n", s1.roll_no, s1.name, s1.marks);
    
    printf("Student Details via Pointer Arrow Operator:\n");
    printf("Roll: %d | Name: %s | Marks: %.1f\n", ptr->roll_no, ptr->name, ptr->marks);
    return 0;
}
💡 Note:Demonstrates struct declaration, initialization, member access via dot (.) and pointer arrow (->) operators.

🎯 University Exam Scoring Blueprint

  • Always draw the memory layout comparing struct vs union.
  • Explain static variables: they persist their value between function calls and are initialized only once.
  • Explain why the address-of operator (&) cannot be used on register variables (they reside in CPU registers, not RAM).

Top Viva Questions on Structures (struct), Unions, typedef & 4 Storage Classes

1 Questions
1

Why is the arrow operator (->) used with structure pointers?