🔥 Top 70 C Programming Viva Questions with Detailed Answers – Part 3
(Pointers, Dynamic Memory & Advanced Concepts – Questions 42–70)
📌 Read Part 1 (Questions 1–25) here: [https://learninggrowthhub.blogspot.com/2026/02/top-70-c-programming-viva-questions.html]
📌 Read Part 2 (Questions 26–42) here: [https://learninggrowthhub.blogspot.com/2026/02/%20Top%2070%20C%20Programming%20Viva%20Questions%20with%20Detailed%20Answers%20%20Part%202_0831447901.html]
🚀 Introduction
Welcome to the final part of the Top 70 C Programming Viva Series.
In this section, we will master:
Deep Pointer Concepts
Memory Management
Stack vs Heap
Dynamic Allocation
Function Pointers
Double Pointers
Segmentation Fault
Real Interview Trap Questions
⚡ If you understand this section clearly, you are ahead of most C learners.
✅ Q43. What is a Pointer in C?
🎯 Detailed Answer:
A pointer is a variable that stores the memory address of another variable instead of storing a normal value.
Every variable in C is stored in memory. A pointer helps us access and manipulate that memory directly.
👉 In simple words:
A pointer tells us where a value is stored, not the value itself.
💻 Program (Each Line Commented)
#include <stdio.h> // Include standard input-output library
int main() // Main function starts
{
int x = 10; // Declare integer variable x and assign value 10
int *p; // Declare pointer variable that can store address of an integer
p = &x; // Store address of variable x into pointer p
printf("Value of x: %d\n", x); // Print value stored in x
printf("Address of x: %p\n", &x); // Print memory address of x using %p
printf("Value using pointer: %d\n", *p); // Dereference pointer to print value
return 0; // Indicate successful program execution
}
🧠 Time Complexity: O(1)
⚠ Edge Case: If pointer is not initialized and used → segmentation fault
❌ Common Mistake: Confusing *p (value) with p (address)
💡 Viva Tip: Clearly explain difference between address and value
🚀 Interview Insight: Most pointer discussions start from this question
✅ Q44. What is Dereferencing?
🎯 Detailed Answer:
Dereferencing means accessing the value stored at the memory address using * operator.
If:
p = &x
Then:
*p gives value of x
👉 When we use *p, we are saying:
“Go to the memory location stored in p and give me the value.”
💻 Program (Each Line Commented)
#include <stdio.h> // Include standard input-output header
int main() // Main function starts
{
int x = 25; // Declare integer variable x and initialize with 25
int *p = &x; // Declare pointer p and store address of x
*p = 50; // Modify value at address stored in p (changes x to 50)
printf("%d", x); // Print updated value of x (prints 50)
return 0; // End program successfully
}
🧠 Time Complexity: O(1)
⚠ Edge Case: Dereferencing NULL pointer → crash
❌ Common Mistake: Forgetting * while accessing value
💡 Viva Tip: Explain that dereferencing modifies original variable
✅ Q45. Explain Pointer Arithmetic
🎯 Detailed Answer:
Pointer arithmetic allows movement through memory locations.
If pointer is int *p, and we do:
p = p + 1
It moves by size of int (usually 4 bytes), not 1 byte.
👉 Pointer moves based on data type size.
💻 Program (Each Line Commented)
#include <stdio.h> // Include standard input-output header
int main() // Main function begins
{
int arr[3] = {10, 20, 30}; // Declare integer array of size 3
int *p = arr; // Pointer p stores base address of array
printf("%d\n", *p); // Print first element (arr[0])
p++; // Move pointer to next integer location
printf("%d\n", *p); // Print second element (arr[1])
return 0; // End program
}
🧠 Time Complexity: O(1)
❌ Common Mistake: Thinking pointer increases by 1 byte
💡 Viva Tip: Always mention size of datatype
✅ Q46. Call by Value vs Call by Reference
🎯 Detailed Explanation:
When we pass variables to a function, C can pass them in two ways.
🔹 Call by Value
A copy of the variable is passed.
Changes inside the function do NOT affect the original variable.
Example idea:
x = 5
Function receives copy of 5
Function changes copy to 10
Original x still remains 5
🔹 Call by Reference
Address of variable is passed.
Function directly accesses original memory.
So changes inside function affect original variable.
💻 Program (Swap Using Reference – Fully Commented)
#include <stdio.h> // Include standard input-output header
void swap(int *a, int *b) // Function receives addresses of two integers
{
int temp; // Temporary variable to store value
temp = *a; // Store value at address a
*a = *b; // Replace value at address a with value at address b
*b = temp; // Replace value at address b with temp value
}
int main() // Main function starts
{
int x = 5, y = 10; // Declare two integer variables
swap(&x, &y); // Pass addresses of x and y
printf("%d %d", x, y); // Print swapped values
return 0; // End program
}
🧠 How to Understand Easily
-
&xgives address -
Function receives address
-
*amodifies original memory -
That’s why swap works
🧠 Time Complexity: O(1)
⚠ Edge Case: Passing NULL pointer
❌ Common Mistake: Forgetting * inside function
✅ Q47. What is a NULL Pointer?
🎯 Detailed Explanation:
A NULL pointer does not point to any valid memory location.
int *p = NULL; // Pointer safely initialized to nothing
👉 It prevents accidental access to random memory.
Always set pointer to NULL after free().
🧠 Time Complexity: O(1)
⚠ Edge Case: Dereferencing NULL causes crash
❌ Common Mistake: Thinking NULL means zero value
✅ Q48. What is a Wild Pointer?
🎯 Detailed Explanation:
A pointer declared but not initialized.
int *p; // Wild pointer
It contains garbage address.
If used → program crash.
👉 Always initialize pointer with NULL or valid address.
🧠 Time Complexity: O(1)
❌ Common Mistake: Using pointer before assigning address
✅ Q49. What is a Dangling Pointer?
🎯 Detailed Explanation:
A dangling pointer points to memory that has already been freed.
💻 Program (Each Line Commented)
#include <stdio.h> // Standard input-output header
#include <stdlib.h> // Required for malloc and free
int main() // Main function starts
{
int *p = (int*)malloc(sizeof(int)); // Allocate memory in heap
*p = 100; // Store value in allocated memory
free(p); // Free allocated memory
p = NULL; // Prevent dangling pointer
return 0; // End program
}
🧠 Time Complexity: O(1)
❌ Common Mistake: Using pointer after free()
⚠ Edge Case: Forgetting free() causes memory leak
✅ Q50. What is Dynamic Memory Allocation?
🎯 Detailed Explanation:
Dynamic Memory Allocation means allocating memory during runtime.
Used when size is unknown at compile time.
Functions used:
-
malloc()
-
calloc()
-
realloc()
-
free()
👉 Required for linked list, trees, dynamic arrays.
🧠 Time Complexity: O(1)
⚠ Edge Case: Allocation failure
❌ Common Mistake: Forgetting free()
✅ Q51. Write Program Using malloc()
🎯 Detailed Explanation:
malloc allocates memory but does not initialize it.
Always check if malloc returns NULL.
💻 Program (Each Line Commented)
#include <stdio.h> // Include input-output header
#include <stdlib.h> // Include memory allocation functions
int main() // Main function begins
{
int *p; // Declare pointer
p = (int*)malloc(sizeof(int)); // Allocate memory for one integer
if(p == NULL) // Check allocation failure
{
printf("Memory not allocated"); // Error message
return 0; // Exit program
}
*p = 25; // Store value in allocated memory
printf("%d", *p); // Print stored value
free(p); // Free allocated memory
p = NULL; // Prevent dangling pointer
return 0; // End program
}
🧠 Time Complexity: O(1)
⚠ Edge Case: malloc returns NULL
❌ Common Mistake: Not checking NULL
✅ Q52. What is a Pointer in C?
📘 Explanation
A pointer is a variable that stores the memory address of another variable.
& → Address-of operator
-
→ Dereference operator
Pointer improves efficiency and dynamic memory usage
💻 Program: Basic Pointer Example
#include <stdio.h> // Includes standard input-output library for printf
int main() // Main function starts execution
{
int num = 10; // Declare integer variable and initialize with 10
int *ptr; // Declare a pointer variable that can store address of int
ptr = # // Store address of variable num into pointer ptr
printf("Value of num: %d\n", num); // Print value stored in num
printf("Address of num: %p\n", &num); // Print memory address of num
printf("Pointer value: %p\n", ptr); // Print address stored inside pointer
printf("Value using pointer: %d\n", *ptr); // Dereference pointer to get value at stored address
return 0; // Return 0 indicates successful program execution
}
🧠 Time Complexity: O(1)
⚠️ Edge Case: Uninitialized pointer causes garbage access
❌ Common Mistake: Forgetting to initialize pointer before use
✅ Q53. What is NULL Pointer?
📘 Explanation
A NULL pointer does not point to any valid memory location.
It is used to indicate “no address”.
💻 Program
#include <stdio.h> // Standard input-output library
#include <stdlib.h> // Standard library (defines NULL)
int main() // Main function
{
int *ptr = NULL; // Initialize pointer with NULL (points to nothing)
if(ptr == NULL) // Check whether pointer is NULL
{
printf("Pointer is NULL\n"); // Print message if pointer is NULL
}
return 0; // End of program
}
🧠 Time Complexity: O(1)
⚠️ Edge Case: Dereferencing NULL causes crash
❌ Common Mistake: Using pointer without checking NULL
✅ Q54. What is Dangling Pointer?
📘 Explanation
A dangling pointer points to memory that has already been freed.
💻 Program Example
#include <stdio.h> // Standard input-output header
#include <stdlib.h> // Required for malloc() and free()
int main() // Main function
{
int *ptr = (int*)malloc(sizeof(int)); // Allocate memory for one integer
*ptr = 5; // Store value 5 at allocated memory location
free(ptr); // Free the allocated memory
ptr = NULL; // Assign NULL to avoid dangling pointer
return 0; // End program
}
🧠 Time Complexity: O(1)
⚠️ Edge Case: Accessing freed memory causes undefined behavior
❌ Common Mistake: Not setting pointer to NULL after free
✅ Q55. Program Using malloc()
#include <stdio.h> // Standard input-output header
#include <stdlib.h> // Required for malloc() and free()
int main() // Main function
{
int n, i; // Declare variables for size and loop counter
printf("Enter number of elements: "); // Ask user for input size
scanf("%d", &n); // Read number of elements from user
int *arr = (int*)malloc(n * sizeof(int)); // Dynamically allocate memory for n integers
if(arr == NULL) // Check if memory allocation failed
{
printf("Memory not allocated\n"); // Print error message
return 1; // Exit program with error code
}
for(i = 0; i < n; i++) // Loop to take input
{
scanf("%d", &arr[i]); // Store input value in allocated memory
}
for(i = 0; i < n; i++) // Loop to print values
{
printf("%d ", arr[i]); // Print each element
}
free(arr); // Free allocated memory to prevent memory leak
return 0; // Successful termination
}
🧠 Time Complexity: O(n)
⚠️ Edge Case: If n = 0, allocation size becomes zero
❌ Common Mistake: Forgetting to free memory
✅ Q56. What is realloc()?
💻 Program
#include <stdio.h> // Standard input-output header
#include <stdlib.h> // Required for malloc() and realloc()
int main() // Main function
{
int *ptr; // Declare pointer
int n = 3, i; // Initial size is 3
ptr = (int*)malloc(n * sizeof(int)); // Allocate memory for 3 integers
for(i = 0; i < n; i++) // Initialize allocated memory
ptr[i] = i + 1; // Assign values 1, 2, 3
n = 5; // Increase size to 5
ptr = (int*)realloc(ptr, n * sizeof(int)); // Resize memory block
for(i = 3; i < n; i++) // Initialize newly allocated memory
ptr[i] = i + 1; // Assign values 4 and 5
for(i = 0; i < n; i++) // Print all elements
printf("%d ", ptr[i]); // Output values
free(ptr); // Free allocated memory
return 0; // End program
}
🧠 Time Complexity: O(n)
⚠️ Edge Case: realloc may return NULL
❌ Common Mistake: Overwriting original pointer without checking
✅ Q57. What is Structure in C?
💻 Program
#include <stdio.h> // Standard input-output header
struct Student // Define structure named Student
{
int roll; // Integer member roll number
char name[20]; // Character array for name
float marks; // Float variable for marks
};
int main() // Main function
{
struct Student s; // Declare structure variable
printf("Enter roll, name and marks: "); // Prompt user
scanf("%d %s %f", &s.roll, s.name, &s.marks); // Take input
printf("Roll: %d\n", s.roll); // Print roll number
printf("Name: %s\n", s.name); // Print name
printf("Marks: %.2f\n", s.marks); // Print marks up to 2 decimal
return 0; // End program
}
🧠 Time Complexity: O(1)
⚠️ Edge Case: Buffer overflow in string input
❌ Common Mistake: Forgetting & for non-string members
✅ Q58. What is Pointer to Structure?
#include <stdio.h> // Standard input-output header
struct Student // Define structure
{
int roll; // Roll number
float marks; // Marks
};
int main() // Main function
{
struct Student s = {1, 90.5}; // Initialize structure variable
struct Student *ptr = &s; // Pointer stores address of structure
printf("Roll: %d\n", ptr->roll); // Access roll using arrow operator
printf("Marks: %.2f\n", ptr->marks); // Access marks using arrow operator
return 0; // End program
}
🧠 Time Complexity: O(1)
⚠️ Edge Case: Accessing via NULL pointer
❌ Common Mistake: Using . instead of ->
✅ Q59. Program to Write to File
#include <stdio.h> // Standard input-output header
int main() // Main function
{
FILE *fp = fopen("data.txt", "w"); // Open file in write mode
if(fp == NULL) // Check if file failed to open
{
printf("File not opened\n"); // Error message
return 1; // Exit program
}
fprintf(fp, "Hello Krishna"); // Write text into file
fclose(fp); // Close file to save changes
return 0; // End program
}
🧠 Time Complexity: O(n)
⚠️ Edge Case: File permission issue
❌ Common Mistake: Not checking NULL
✅ Q60. Program to Read from File
#include <stdio.h> // Standard input-output header
int main() // Main function
{
FILE *fp = fopen("data.txt", "r"); // Open file in read mode
char ch; // Variable to store each character
if(fp == NULL) // Check if file exists
{
printf("File not found\n"); // Print error
return 1; // Exit program
}
while((ch = fgetc(fp)) != EOF) // Read character until End Of File
{
printf("%c", ch); // Print each character
}
fclose(fp); // Close file
return 0; // End program
}
🧠 Time Complexity: O(n)
⚠️ Edge Case: Empty file
❌ Common Mistake: Comparing char with EOF incorrectly
✅ Q61. What is Double Pointer in C?
📘 Explanation
A double pointer is a pointer that stores the address of another pointer.
Syntax:
int **ptr;
It is mainly used in:
-
Dynamic memory (2D allocation)
-
Function arguments (modify pointer inside function)
💻 Program
#include <stdio.h> // Standard input-output header
int main() // Main function
{
int num = 20; // Normal integer variable
int *ptr = # // Pointer storing address of num
int **dptr = &ptr; // Double pointer storing address of ptr
printf("Value of num: %d\n", num); // Print value of num
printf("Value using ptr: %d\n", *ptr); // Dereference single pointer
printf("Value using dptr: %d\n", **dptr); // Dereference double pointer
return 0; // End program
}
🧠 Time Complexity: O(1)
⚠️ Edge Case: If pointer is NULL, double dereferencing crashes
❌ Common Mistake: Confusing *ptr and **ptr
✅ Q62. Passing Pointer to Function
📘 Explanation
Pointers allow functions to modify original variable values.
This is called Call by Reference.
💻 Program (Swap using Pointer)
#include <stdio.h> // Standard input-output header
void swap(int *a, int *b) // Function with pointer parameters
{
int temp; // Temporary variable
temp = *a; // Store value of a
*a = *b; // Assign value of b to a
*b = temp; // Assign stored value to b
}
int main() // Main function
{
int x = 5, y = 10; // Initialize variables
swap(&x, &y); // Pass addresses to function
printf("x = %d\n", x); // Print swapped value
printf("y = %d\n", y); // Print swapped value
return 0; // End program
}
🧠 Time Complexity: O(1)
⚠️ Edge Case: Passing NULL instead of valid address
❌ Common Mistake: Forgetting & while passing
✅ Q63. What is Memory Leak?
📘 Explanation
Memory leak occurs when dynamically allocated memory is not freed.
It wastes heap memory and reduces performance.
💻 Program (Memory Leak Example)
#include <stdio.h> // Standard input-output header
#include <stdlib.h> // Required for malloc()
int main() // Main function
{
int *ptr = (int*)malloc(sizeof(int)); // Allocate memory
*ptr = 100; // Assign value
printf("%d\n", *ptr); // Print value
// free(ptr); // Memory not freed → Memory leak
return 0; // Program ends but memory not released
}
🧠 Time Complexity: O(1)
⚠️ Edge Case: Continuous allocation causes crash
❌ Common Mistake: Forgetting free()
✅ Q64. Difference Between Stack and Heap Memory (Program Demo)
📘 Explanation
Stack:
-
Automatic memory
-
Fast
-
Function variables
Heap:
-
Dynamic memory
-
Slower
-
Manual management
💻 Program
#include <stdio.h> // Standard input-output header
#include <stdlib.h> // Required for malloc()
int main() // Main function
{
int a = 10; // Stack memory variable
int *ptr = (int*)malloc(sizeof(int)); // Heap memory allocation
*ptr = 20; // Assign value to heap memory
printf("Stack value: %d\n", a); // Print stack variable
printf("Heap value: %d\n", *ptr); // Print heap variable
free(ptr); // Free heap memory
return 0; // End program
}
🧠 Time Complexity: O(1)
⚠️ Edge Case: Not freeing heap memory
❌ Common Mistake: Thinking heap auto-cleans
✅ Q65. What is Recursive Function? (Factorial Example)
📘 Explanation
Recursion is when a function calls itself.
Must have:
✔ Base condition
✔ Recursive call
💻 Program
#include <stdio.h> // Standard input-output header
int factorial(int n) // Recursive function
{
if(n == 0 || n == 1) // Base condition
return 1; // Return 1
return n * factorial(n - 1); // Recursive call
}
int main() // Main function
{
int num = 5; // Initialize number
printf("Factorial: %d", factorial(num)); // Call function
return 0; // End program
}
🧠 Time Complexity: O(n)
⚠️ Edge Case: Large n causes stack overflow
❌ Common Mistake: Missing base condition
✅ Q66. What is Segmentation Fault?
📘 Explanation
Segmentation fault occurs when a program accesses invalid memory.
Reasons:
-
Dereferencing NULL
-
Accessing freed memory
-
Invalid pointer access
💻 Program Example
#include <stdio.h> // Standard input-output header
int main() // Main function
{
int *ptr = NULL; // Initialize pointer to NULL
*ptr = 10; // Invalid access → Segmentation fault
return 0; // Program will crash before this
}
🧠 Time Complexity: O(1)
⚠️ Edge Case: Accessing invalid memory
❌ Common Mistake: Not checking pointer before dereferencing
✅ Q67. What is Command Line Argument?
📘 Explanation
Command line arguments allow passing values while running program.
Syntax:
int main(int argc, char *argv[])
-
argc → Argument count
-
argv → Argument vector
💻 Program
#include <stdio.h> // Standard input-output header
int main(int argc, char *argv[]) // Main with arguments
{
printf("Number of arguments: %d\n", argc); // Print count
for(int i = 0; i < argc; i++) // Loop through arguments
{
printf("Argument %d: %s\n", i, argv[i]); // Print each argument
}
return 0; // End program
}
🧠 Time Complexity: O(n)
⚠️ Edge Case: No arguments passed
❌ Common Mistake: Accessing argv without checking argc
✅ Q68. What is Function Pointer?
📘 Explanation
A function pointer stores the address of a function.
Used in:
-
Callback functions
-
Menu-driven programs
💻 Program
#include <stdio.h> // Standard input-output header
int add(int a, int b) // Normal function
{
return a + b; // Return sum
}
int main() // Main function
{
int (*ptr)(int, int); // Declare function pointer
ptr = add; // Store function address
printf("Sum: %d", ptr(5, 3)); // Call function using pointer
return 0; // End program
}
🧠 Time Complexity: O(1)
⚠️ Edge Case: Incorrect function signature
❌ Common Mistake: Wrong pointer declaration syntax
✅ Q69. Program to Free Nested Dynamic Memory
📘 Explanation
When dynamic memory contains pointers inside structures, every allocated block must be freed.
This prevents memory leaks and dangling pointers.
💻 Program
#include <stdio.h> // Standard input-output header
#include <stdlib.h> // Required for malloc() and free()
struct Student // Structure with pointer member
{
char *name;
int marks;
};
int main() // Main function
{
struct Student *s = (struct Student*)malloc(sizeof(struct Student)); // Allocate structure
s->name = (char*)malloc(20 * sizeof(char)); // Allocate memory for name
// Assign values
s->marks = 90;
snprintf(s->name, 20, "Krishna"); // Store string safely
printf("Name: %s\n", s->name); // Print name
printf("Marks: %d\n", s->marks); // Print marks
free(s->name); // Free inner pointer memory
free(s); // Free structure memory
return 0; // End program
}
🧠 Time Complexity: O(1)
⚠️ Edge Case: Forgetting to free inner pointer → memory leak
❌ Common Mistake: Freeing structure before inner pointers
✅ Q70. Program to Open File in Append Mode
📘 Explanation
File append mode "a" allows writing new data at the end of an existing file without overwriting it.
💻 Program
#include <stdio.h> // Standard input-output header
int main() // Main function
{
FILE *fp = fopen("data.txt", "a"); // Open file in append mode
if(fp == NULL) // Check if file opened successfully
{
printf("File not opened\n"); // Error message
return 1; // Exit program
}
fprintf(fp, "\nAdding new line"); // Write new line at end
fclose(fp); // Close file
printf("Data appended successfully\n"); // Confirmation
return 0; // End program
}
🧠 Time Complexity: O(n)
⚠️ Edge Case: File permission issue
❌ Common Mistake: Using "w" instead of "a" overwrites file
🎯 Final Conclusion – Top 70 C Programming Viva Questions Series
Congratulations! You’ve completed the Top 70 C Programming Viva Questions series, covering all essential concepts from the basics to advanced topics.
Here’s what you’ve mastered in this series:
-
Variables, Data Types, and Operators – Foundation of C programming
-
Control Statements and Loops – Execution flow and logic
-
Functions and Recursion – Modular and reusable code
-
Arrays, Strings, and Pointers – Efficient memory handling and dynamic access
-
Dynamic Memory Allocation – malloc, calloc, realloc, free, and memory management
-
Structures and Unions – Organizing complex data
-
File Handling – Persistent data storage
-
Advanced Pointer Concepts – Double pointers, function pointers, NULL, dangling, and wild pointers
-
Memory Concepts – Stack vs Heap, Memory Leak, Segmentation Fault
-
Command Line Arguments – Making programs flexible and user-driven
⚡ Key Takeaways:
-
Pointers and dynamic memory are the backbone of advanced C programming.
-
Understanding memory management prevents crashes and improves performance.
-
Structures, unions, and file handling allow building real-world applications.
-
Recursion and function pointers are frequently asked in interviews.
-
Mastery of these 70 questions puts you ahead of most C learners and prepares you for technical interviews confidently.
✅ Pro Tip for Interviews:
-
Always explain concepts clearly, give examples, and discuss time complexity.
-
Highlight edge cases and common mistakes—it shows deep understanding.
By revising these questions and practicing the programs line by line, you are now fully equipped to tackle any C programming viva or coding interview.
📌 Keep Learning. Keep Coding. Keep Growing.
✨ Written by Krishna Popat
🌱 Founder, Learning Growth Hub
Comments
Post a Comment