Programming for Problem Solving (ESCS201) · Curated Top-Frequency Exam Questions
auto, static, extern, register) with their default initial values, scope, and lifetime.
[ 10 Marks ]
| Storage Class | Storage Location | Default Initial Value | Scope (Visibility) | Lifetime |
|---|---|---|---|---|
auto |
RAM Call Stack | Garbage Value (random uninitialized bytes) | Local (within the enclosing function/block only) | Destroyed automatically upon block exit. |
static |
RAM Data Segment | Zero (0) |
Local to the declaring function/block (or file if global) | Persists across function calls for entire program runtime. |
extern |
RAM Data Segment | Zero (0) |
Global (accessible across all translation units/files) | Entire program execution runtime. |
register |
CPU Registers (if available) | Garbage Value | Local to block | Destroyed upon block exit. Note: Cannot take address ®. |
Type Conversion (Implicit Conversion): Automatically performed by the C compiler when operands of different data types are mixed in an expression. Smaller types are automatically promoted to larger types (e.g., int to float) to prevent data loss.
Type Casting (Explicit Conversion): Manually performed by the programmer using the cast operator (data_type)expr to forcibly convert an expression from one data type to another.
malloc, calloc, realloc, free) in C with examples.
[ 10 Marks ]
Dynamic memory allocation functions (defined in <stdlib.h>) allocate heap memory at runtime:
malloc(size_t size): Allocates a single contiguous block of memory of specified size in bytes. Contains uninitialized garbage values.
int *ptr = (int*)malloc(10 * sizeof(int));calloc(size_t num, size_t size): Allocates memory for an array of num elements of size bytes each, and automatically initializes all bytes to zero.
int *arr = (int*)calloc(10, sizeof(int));realloc(void *ptr, size_t new_size): Resizes an already allocated memory block without losing previously stored data (expanding or shrinking).
ptr = (int*)realloc(ptr, 20 * sizeof(int));free(void *ptr): Deallocates heap memory previously allocated by malloc/calloc/realloc, returning it to the operating system to prevent memory leaks.
free(ptr); ptr = NULL;1. Array of Pointers (int *arr[5];): This is an array containing 5 pointer elements. Every element arr[i] stores the memory address of an integer variable.
2. Pointer to an Array (int (*ptr)[5];): Notice the parentheses around *ptr. This declares a single pointer variable that points to an entire 1-dimensional array of 5 integers. When incremented (ptr++), its memory address jumps by \(5 \times \text{sizeof(int)} = 20\) bytes!
A self-referential structure is a structure definition that includes one or more pointer members pointing to a structure of the exact same type. They form the foundational building blocks for dynamic data structures like Linked Lists, Trees, and Graphs.