C Programming Study Material
1. Basic Concepts
What is a pointer in C, and why is it useful?
- A pointer is a variable that stores the memory address of another variable. It allows access and manipulation of
memory, supports dynamic memory allocation, and enables complex data structures like linked lists.
Difference between malloc() and calloc():
- malloc(size) allocates a single block of memory without initializing it.
- calloc(n, size) allocates memory for an array and initializes it to zero.
Keywords: static, extern, and volatile:
- static: Limits the scope of a variable or function.
- extern: Declares a global variable defined in another file.
- volatile: Prevents the compiler from optimizing a variable as its value can change unexpectedly.
Pass by value vs. Pass by reference:
- Pass by value: A copy of the variable is passed to the function.
- Pass by reference: A pointer to the variable is passed.
Memory allocation: Stack vs. Heap:
- Stack: For static memory allocation, managed automatically.
- Heap: For dynamic memory allocation, managed manually using malloc(), free().
2. Control Structures
How do you write conditional statements in C?
- if-else: if (condition) { ... } else { ... }
- switch-case: switch (expression) { case value: ... break; default: ... }
Difference between while, do-while, and for loops:
- while: Checks the condition before each iteration.
- do-while: Executes the loop at least once before checking.
- for: Executes a set number of iterations.