Unit 3 of MAKAUT C Programming (PPS-II) shifts focus from basic control flow to structured modular software design. Pointers, call-by-reference mechanics, storage classes (static, extern), and 1D array operations are heavily tested in both university semester examinations and campus technical interviews (TCS NQT, Cognizant, Wipro, LTIMindtree).
This guide unpacks the entire Unit 3 syllabus with clear stack activation frame diagrams, storage class memory tables, and 5+ solved high-scoring MAKAUT exam programs.
---
1. Modular Programming & Functions
A function is a self-contained block of statements designed to perform a specific task.
Components of a Function:
- Function Prototype: Informs compiler of function name, parameters, and return type before invocation.
- Function Definition: Contains actual executable code body.
- Function Call: Invokes the function, passing actual arguments and allocating a stack frame.
---
2. Storage Classes in C: auto, register, static, extern
Storage classes dictate 4 properties of a variable: Storage Location (RAM/Register), Default Initial Value, Scope, and Lifetime. Questions asking to differentiate between static and extern appear almost every year in Group B (5 Marks).
| Storage Class | Location | Default Initial Value | Scope | Lifetime |
|---|---|---|---|---|
| `auto` | Stack RAM | Garbage Value | Local to block | Block execution duration |
| `register` | CPU Register (if free) | Garbage Value | Local to block | Block execution duration |
| `static` | Data Segment (RAM) | Zero (0) | Local to block | Retained across function calls |
| `extern` | Data Segment (RAM) | Zero (0) | Global across files | Entire application lifetime |
---
3. Parameter Passing: Call by Value vs Call by Reference
1Call by Value(Values Copied):2Main Stack Frame [x = 10, y = 20] ---> Copy ---> Function Stack Frame [a = 10, b = 20]34Call by Reference(Memory Addresses Passed):5Main Stack Frame [x = 10, y = 20] <--- Dereference --- Function Stack Frame [pA = &x, pB = &y]
Swapping Program: Call by Value vs Call by Reference
1"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>23void swapByValue(int a, int b) {4 int temp = a;5 a = b;6 b = temp;7}89void swapByReference(int *a, int *b) {10 int temp = *a;11 *a = *b;12 *b = temp;13}1415int main() {16 int x = 10, y = 20;1718 swapByValue(x, y);19 printf("After Call by Value: x = %d, y = %d\n", x, y); // Unchanged: 10, 202021 swapByReference(&x, &y);22 printf("After Call by Reference: x = %d, y = %d\n", x, y); // Swapped: 20, 102324 return 0;25}
---
4. 1D Array Fundamentals & Pointer-Array Duality
An array stores elements of the same data type in contiguous memory locations.
Key Equivalences:
arr(array name) decays into pointer to first element (&arr[0]).arr[i]\equiv*(arr + i).&arr[i]\equiv(arr + i).
---
5. Solved High-Scoring MAKAUT Exam Programs
Program 1: Finding 2nd Largest & 2nd Smallest Array Elements (Single Pass O(N))
*Exam Context: Group B / Group C (10 Marks)*
1"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>2"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><limits.h>34int main() {5 int arr[] = {12, 35, 1, 10, 34, 1};6 int n = sizeof(arr) / sizeof(arr[0]);78 int max1 = INT_MIN, max2 = INT_MIN;9 int min1 = INT_MAX, min2 = INT_MAX;1011 for (int i = 0; i < n; i++) {12 // Update Max values13 if (arr[i] > max1) {14 max2 = max1;15 max1 = arr[i];16 } else if (arr[i] > max2 && arr[i] != max1) {17 max2 = arr[i];18 }1920 // Update Min values21 if (arr[i] < min1) {22 min2 = min1;23 min1 = arr[i];24 } else if (arr[i] < min2 && arr[i] != min1) {25 min2 = arr[i];26 }27 }2829 printf("2nd Largest Element = %d\n", max2);30 printf("2nd Smallest Element = %d\n", min2);31 return 0;32}
---
Program 2: Merging Two Sorted Arrays into a Third Sorted Array
*Exam Context: Group C (10 Marks)*
1"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>23void mergeSortedArrays(int arr1[], int n1, int arr2[], int n2, int result[]) {4 int i = 0, j = 0, k = 0;56 while (i < n1 && j < n2) {7 if (arr1[i] <= arr2[j]) {8 result[k++] = arr1[i++];9 } else {10 result[k++] = arr2[j++];11 }12 }1314 // Copy remaining elements15 while (i < n1) result[k++] = arr1[i++];16 while (j < n2) result[k++] = arr2[j++];17}1819int main() {20 int arr1[] = {1, 3, 5, 7};21 int arr2[] = {2, 4, 6, 8, 10};22 int n1 = 4, n2 = 5;23 int result[9];2425 mergeSortedArrays(arr1, n1, arr2, n2, result);2627 printf("Merged Sorted Array: ");28 for (int i = 0; i < 9; i++) {29 printf("%d ", result[i]);30 }31 printf("\n");32 return 0;33}
---
Program 3: Adam Number Verification
An Adam Number is a number where the square of the reverse of the number equals the reverse of the square of the number (e.g., 12^2 = 144; reverse of 12 is 21; 21^2 = 441; reverse of 441 is 144).
1"text-[#cf222e] dark:text-[#c586c0] font-semibold">#include "text-[#0a3069] dark:text-[#ce9178]"><stdio.h>23int reverse(int num) {4 int rev = 0;5 while (num > 0) {6 rev = rev * 10 + (num % 10);7 num /= 10;8 }9 return rev;10}1112int main() {13 int num;14 printf("Enter integer: ");15 scanf("%d", &num);1617 int sq1 = num * num;18 int revNum = reverse(num);19 int sq2 = revNum * revNum;2021 if (sq1 == reverse(sq2))22 printf("%d is an Adam Number.\n", num);23 else24 printf("%d is NOT an Adam Number.\n", num);2526 return 0;27}
---
6. Key Takeaways for Unit 3
- Never return pointers to local automatic stack variables from a function. Local stack memory is deallocated upon function return, resulting in a dangling pointer.
- Pass large arrays via pointers (
const int *arr) to avoid overhead from copying array elements.