list of 25 important C programming interview questions and answers tailored for experienced candidates. These cover advanced concepts, memory management, pointers, and best practices.
1. What is the difference between malloc()
, calloc()
, and realloc()
?
Answer:
malloc()
: Allocates memory of a given size (uninitialized).calloc()
: Allocates memory for an array of elements and initializes them to zero.realloc()
: Resizes previously allocated memory (can expand or shrink).
2. Explain the difference between const char* p
and char* const p
.
Answer:
const char* p
: Pointer to a constant character (value cannot be modified).char* const p
: Constant pointer to a character (pointer cannot point elsewhere).
3. What is a dangling pointer? How can it be avoided?
Answer:
A dangling pointer points to a deallocated memory location.
Avoid by:
- Setting pointers to
NULL
afterfree()
. - Avoiding returning local variable addresses.
- Using dynamic memory carefully.
4. How does volatile
keyword work in C?
Answer:volatile
tells the compiler that a variable may change unexpectedly (e.g., by hardware or interrupts), preventing compiler optimizations.
5. What is the use of restrict
keyword in C?
Answer:restrict
indicates that a pointer is the only way to access the memory it points to, enabling compiler optimizations.
6. Explain the difference between #include <file.h>
and #include "file.h"
.
Answer:
#include <file.h>
: Searches in standard system directories.#include "file.h"
: Searches in the current directory first, then system directories.
7. What is a memory leak? How can it be detected and prevented?
Answer:
A memory leak occurs when dynamically allocated memory is not freed.
Detection: Tools like Valgrind, AddressSanitizer.
Prevention: Always free()
allocated memory, use smart pointers (in C++), or RAII techniques.
8. What is the purpose of typedef
in C?
Answer:typedef
creates an alias for existing types, improving readability.
Example:
typedef unsigned int uint;
9. How does sizeof()
work for pointers and arrays?
Answer:
- For a pointer: Returns the size of the pointer (e.g.,
sizeof(int*)
→ 4 or 8 bytes). - For an array: Returns the total size of the array (e.g.,
sizeof(arr)
forint arr[10]
→40
bytes).
10. What is a function pointer? Give an example.
Answer:
A function pointer stores the address of a function.
Example:
int (*funcPtr)(int, int) = &add; // Points to function 'add'
11. Explain the difference between strcpy()
and strncpy()
.
Answer:
strcpy()
copies until\0
(unsafe if destination is smaller).strncpy()
copies up ton
characters (safer but may not null-terminate).
12. What is the stack and heap? How are they different?
Answer:
- Stack: Stores local variables, automatically managed (LIFO), faster but limited size.
- Heap: Dynamic memory allocation (
malloc
,free
), larger but slower and manual management.
13. What is a segmentation fault? Common causes?
Answer:
A segfault occurs when accessing invalid memory.
Causes:
- Dereferencing
NULL
or wild pointers. - Buffer overflow.
- Stack overflow.
14. How do you implement a linked list in C?
Answer:
struct Node {
int data;
struct Node* next;
};
15. What is the difference between ++i
and i++
?
Answer:
++i
(pre-increment): Increments first, then uses the value.i++
(post-increment): Uses the value first, then increments.
16. Explain static
variables in C.
Answer:
- Static local variables: Retain value between function calls.
- Static global variables: Limited to the file scope (not accessible externally).
17. What is #pragma once
? How is it different from include guards?
Answer:
#pragma once
: Non-standard but efficient (prevents multiple inclusions).- Include guards: Standard (
#ifndef HEADER_H
), works everywhere.
18. How does qsort()
work in C?
Answer:
void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
It sorts an array using a comparator function.
19. What is the difference between struct
and union
?
Answer:
- Struct: Allocates memory for all members (size = sum of members).
- Union: Allocates memory for the largest member (size = largest member).
20. How do you use setjmp()
and longjmp()
?
Answer:
They provide non-local jumps (like exception handling).
jmp_buf env;
if (setjmp(env) { /* Error handling */ }
else { longjmp(env, 1); }
21. What is inline
function in C?
Answer:inline
suggests the compiler to embed the function code at the call site (avoiding function call overhead).
22. Explain the restrict
keyword with an example.
Answer:
void copy(int *restrict dest, int *restrict src, int n) { ... }
Ensures dest
and src
do not overlap, allowing optimizations.
23. What is the purpose of assert()
in C?
Answer:assert()
checks if a condition is true; if not, it aborts the program (used for debugging).
24. How do you reverse a string in C?
Answer:
void reverse(char *str) {
int n = strlen(str);
for (int i = 0; i < n/2; i++) {
char temp = str[i];
str[i] = str[n-i-1];
str[n-i-1] = temp;
}
}
25. What are variadic functions? Give an example.
Answer:
Functions that take a variable number of arguments (e.g., printf
).
Example:
#include <stdarg.h>
int sum(int count, ...) {
va_list args;
va_start(args, count);
int total = 0;
for (int i = 0; i < count; i++) {
total += va_arg(args, int);
}
va_end(args);
return total;
}
These questions cover memory management, pointers, compiler directives, and best practices, making them ideal for experienced C programmers.
Here are section-wise titles for your article to make it well-structured and SEO-friendly:
1. Memory Management & Dynamic Allocation
- What is the difference between
malloc()
,calloc()
, andrealloc()
? - What is a memory leak? How can it be detected and prevented?
- Explain the difference between the stack and the heap.
2. Pointers & Advanced Concepts
- What is a dangling pointer? How can it be avoided?
- Explain the difference between
const char* p
andchar* const p
. - What is the use of the
restrict
keyword in C? - How does
sizeof()
work for pointers and arrays? - What is a function pointer? Give an example.
3. Compiler Directives & Preprocessor
- Explain the difference between
#include <file.h>
and#include "file.h"
. - What is
#pragma once
? How is it different from include guards? - What is the purpose of
typedef
in C?
4. Storage Classes & Variable Modifiers
- Explain
static
variables in C. - How does the
volatile
keyword work in C? - What is an
inline
function in C?
5. Strings & Arrays
- Explain the difference between
strcpy()
andstrncpy()
. - How do you reverse a string in C?
- How does
qsort()
work in C?
6. Structures, Unions & Data Types
- What is the difference between a
struct
and aunion
? - How do you implement a linked list in C?
7. Control Flow & Error Handling
- What is the difference between
++i
andi++
? - How do you use
setjmp()
andlongjmp()
? - What is the purpose of
assert()
in C?
8. Advanced Functions & Utilities
- What are variadic functions? Give an example.
- Explain the
restrict
keyword with an example.
9. Common Pitfalls & Debugging
- What is a segmentation fault? What are its common causes?
- TAGS : CProgramming , #CInterviewQuestions , #CInterviewPrep , #CProgrammingForExperienced , #ProgrammingInterviews , #MemoryManagementInC , #PointersInC , #AdvancedCProgramming , #DataStructuresInC , #CLanguageTricks , #SoftwareDevelopment , #TechInterviews , #CodingInterview , #ProgrammingTips , #DeveloperGuide , #EmbeddedC (if targeting firmware/low-level devs) , #LinuxProgramming (if relevant) , #LowLevelProgramming , #AlgorithmInC , #DebuggingInC , #LearnCProgramming , #ProgrammingQA , #CodeNewbie (if beginners might read) , #TechCareers , #DevCommunity