Core Definitions & Key Concepts
Core Definitions & Key Concepts
What is Modular Programming
A software design technique that decomposes a large complex program into smaller, self-contained, independent, and reusable subroutines or functions, improving readability, testing, and team collaboration.
Function Declaration (Prototype) and Function Definition
Prototype: Informs compiler about function name, return type, and parameter list before main() (e.g. int add(int, int);).
Definition: Contains the actual executable body code enclosed in braces.
What is the difference between Actual Parameters and Formal Parameters
Actual Parameters: Values or variables passed to the function in the calling statement (add(x, y)).
Formal Parameters: Variables declared in the function definition header that receive the incoming values (int add(int a, int b)).
Call by Value with its limitation
C passes copies of actual argument values into formal parameters. Changes made to formal parameters inside the function do NOT affect caller variables in main(). Limitation: cannot modify caller arguments directly.
How does C simulate 'Call by Reference'
By passing memory addresses (pointers) of actual arguments into pointer formal parameters (&x), allowing the function to dereference the pointer (*ptr) and modify the original variable.
Recursion and state the two essential components of a recursive function
Recursion is a programming technique where a function calls itself directly or indirectly. Essential components:
- Base Case (Termination Anchor): A non-recursive condition that halts recursion.
- Recursive Step: Progressively reduces the problem toward the base case.
What happens if a recursive function lacks a base case
The function calls itself infinitely until the hardware runtime call stack runs out of memory, crashing the program with a Stack Overflow segmentation fault.
Name the four Storage Classes in C
auto(default for local variables, stack-allocated)register(requests CPU register storage for speed)static(preserves value between function calls, data segment)extern(global scope across multiple source files).
What is the difference between Variable Scope and Variable Lifetime
Scope: The region of the source code where a variable is accessible/visible. Lifetime (Duration): The period of runtime execution during which the variable retains allocated memory storage.
What is the advantage of using a static local variable
A static local variable is initialized only once when execution begins and retains its updated value across multiple calls to that function.
Conceptual & Architectural Analysis
Conceptual & Architectural Analysis
Call by Value versus Call by Reference with a Variable Swapping code example
1#include <stdio.h>2
3// 1. Call by Value (FAILS to swap in caller!)4void swap_by_value(int a, int b) {5 int temp = a; a = b; b = temp;6}7
8// 2. Call by Reference (SUCCEEDS using pointers!)9void swap_by_reference(int *a, int *b) {10 int temp = *a;11 *a = *b;12 *b = temp;13}14
15int main(void) {16 int x = 10, y = 20;17 swap_by_value(x, y);18 printf("After swap_by_value: x = %d, y = %d (Unchanged!)\n", x, y);19
20 swap_by_reference(&x, &y); // Passing memory addresses21 printf("After swap_by_reference: x = %d, y = %d (Swapped!)\n", x, y);22 return 0;23}The Memory Call Stack mechanics during Recursion with an Activation Record diagram
Each function call allocates an Activation Record (Stack Frame) on the call stack containing:
- Return address in caller code.
- Function parameters.
- Local variables.
CALL STACK FOR factorial(3):
+-------------------------------+
| factorial(1): n=1 -> returns 1|
+-------------------------------+
| factorial(2): n=2 * fact(1) |
+-------------------------------+
| factorial(3): n=3 * fact(2) |
+-------------------------------+
| main(): calls factorial(3) |
+-------------------------------+Frames are pushed on each recursive call and popped as base cases return values upwards.
Compare the four Storage Classes in C with a comprehensive comparison matrix
| Storage Class | Keyword | Storage Location | Default Initial Value | Scope | Lifetime |
|---|---|---|---|---|---|
| Automatic | auto | Stack Memory | Garbage (indeterminate) | Local block {} | Exits block |
| Register | register | CPU Register (if available) | Garbage | Local block {} | Exits block |
| Static | static | Data Segment (BSS) | Zero (0) | Local to block or File | Entire program execution |
| External | extern | Data Segment | Zero (0) | Global across all files | Entire program execution |
Write a recursive C function to compute Greatest Common Divisor (GCD) using Euclid's Algorithm
1#include <stdio.h>2
3// Euclidean Algorithm: gcd(a, b) = gcd(b, a % b); base case: gcd(a, 0) = a4int gcd(int a, int b) {5 if (b == 0) return a; // Base condition6 return gcd(b, a % b); // Recursive call7}8
9int main(void) {10 int a = 48, b = 18;11 printf("GCD of %d and %d = %d\n", a, b, gcd(a, b));12 return 0;13}Passing Arrays to Functions in C
In C, arrays are ALWAYS passed by reference (simulated) because the array name decays into a pointer to its first element:
1void print_and_double(int arr[], int size) {2 for (int i = 0; i < size; i++) {3 arr[i] *= 2; // Directly modifies original array in main!4 }5}In-Depth Analysis & Implementation
In-Depth Analysis & Implementation
Solve the classic Tower of Hanoi problem using Recursion in C with complete algorithm explanation and call trace for N = 3 disks
1. Problem Formulation and Recursive Strategy
- Objective: Move N disks from Source peg (A) to Destination peg (C) using Auxiliary peg (B).
- Rules: 1) Move only one disk at a time; 2) A larger disk can never be placed on top of a smaller disk.
- Recursive Insight:
- Move top (N - 1) disks from Source (A) to Auxiliary (B) using Destination (C).
- Move the remaining largest N-th disk directly from Source (A) to Destination (C).
- Move the (N - 1) disks from Auxiliary (B) to Destination (C) using Source (A).
- Total Moves Formula: 2^N - 1. For N = 3, moves = 2^3 - 1 = 7.
2. Complete C Implementation
1#include <stdio.h>2
3void tower_of_hanoi(int n, char from_rod, char to_rod, char aux_rod) {4 if (n == 1) {5 printf("Move disk 1 from rod %c to rod %c\n", from_rod, to_rod);6 return;7 }8 // Step 1: Move n-1 disks from source to aux9 tower_of_hanoi(n - 1, from_rod, aux_rod, to_rod);10
11 // Step 2: Move nth disk from source to dest12 printf("Move disk %d from rod %c to rod %c\n", n, from_rod, to_rod);13
14 // Step 3: Move n-1 disks from aux to dest15 tower_of_hanoi(n - 1, aux_rod, to_rod, from_rod);16}17
18int main(void) {19 int n = 3;20 printf("Tower of Hanoi Solution for %d disks:\n", n);21 tower_of_hanoi(n, 'A', 'C', 'B');22 return 0;23}3. Step-by-Step Execution Trace (N = 3)
- Move disk 1 from A to C
- Move disk 2 from A to B
- Move disk 1 from C to B
- Move disk 3 from A to C
- Move disk 1 from B to A
- Move disk 2 from B to C
- Move disk 1 from A to C
Recursion vs Iteration in depth: Performance, Memory Overhead, Stack Frames, and write recursive vs iterative Factorial code
1. Comparative Analysis Matrix
| Feature | Recursion | Iteration |
|---|---|---|
| Core Mechanism | Function calls itself repeatedly until base condition | Repeats loop construct (for, while) until condition fails |
| Termination | Triggered by base case return | Triggered by loop condition becoming false |
| Memory Consumption | High memory overhead; allocates new stack frame for each call | Constant O(1) memory; reuses existing local variables |
| Execution Speed | Slower due to function call overhead, stack pushing/popping | Significantly faster native hardware loop execution |
| Stack Overflow Risk | High vulnerability if recursion depth exceeds stack limits | No stack overflow risk |
| Code Elegance | Extremely clean for tree traversals, divide-and-conquer (Towers of Hanoi, MergeSort) | Can become verbose and complex for non-linear structures |
2. Code Comparison
1// Iterative: O(N) time, O(1) space2long long fact_iterative(int n) {3 long long res = 1;4 for (int i = 2; i <= n; i++) res *= i;5 return res;6}7
8// Recursive: O(N) time, O(N) stack space9long long fact_recursive(int n) {10 if (n <= 1) return 1;11 return n * fact_recursive(n - 1);12}Scope, Visibility, and Lifetime of Variables in C: Block Scope, Function Scope, File Scope, and External Linkage
1. Scope Dimensions in C
- Block Scope (Local): Variables declared inside
{ }are accessible only within that block. - Function Scope: Labels used with
gotoare visible throughout the entire enclosing function. - File Scope (Global): Variables declared outside all functions are visible from their point of declaration through the end of the file.
2. Multi-File Compilation with extern
1// File 1: global_data.c2int global_counter = 100; // Definition & storage allocation3
4// File 2: main.c5#include <stdio.h>6extern int global_counter; // Declaration: informs linker variable exists elsewhere7
8int main(void) {9 printf("Counter = %d\n", global_counter);10 return 0;11}Important Exam Points & Quick Revision
Important Exam Points & Quick Revision
In BCA-103N Unit 4, the classic exam questions are: Call by Value vs Reference (with swap code), Tower of Hanoi recursive trace, and the 4 Storage Classes comparison table.
Ready to practice Unit 4?
You've read the core concepts. Take the balanced 10-question practice set to test your mastery!