I this post we have provided part 3 of the most common C interview questions along with answers. We recommend you to try to answer the questions, before referring to the given answer. If you find any mistake or a better alternate solution we are happy to see your suggestions and comments

1. How to dynamically allocate memory in C?

Ans : Using malloc() and calloc() functions

2. What is the difference between malloc() and calloc()?

Ans : malloc() takes size of memory segment as argument and calloc() takes size of memory and the number of such segment as argument. malloc() does not clears the returned memory while calloc() does.

3. What is the difference between array and dynamic array?

Ans :
Regular array cannot be resized – dynamic array can be resized
Regular array cannot be deallocated – dynamic array should be deallocated

4. What is memory leak?

Ans : If dynamic memory is allocated and the allocated memory address is lost then it is memory leak as because there is no way standard way to access the memory.

5. What is memory overflow or data corruption?

Ans : Any attempt to write a data of size larger than the available memory will overwrite the value of the next or previous consecutive memory location which is called memory overflow or data corruption

6. What is dangling pointer?

Ans : Address of allocated memory is stored in a pointer and then de-allocated but the address in the pointer is still available which is valid but is not accessible which is called a dangling pointer.

7. What is the advantage of linked list and dynamic array?

Ans : Both linked list and dynamic array can grow and shrink as required, inserting in the middle is less costly in linked list compared to dynamic array and fetching the elements is fast in dynamic array.

8. How to implement the offsetof() macro?

Ans : offsetof(st, m) (&((st *)0->m))

9. How to implement the container_of() macro?

Ans : container_of(ptr, type, member) ({                  \
typeof(((type *)0)->member) __mptr = ptr;             \
(type *)((char *)__mptr – offsetof(type, member)); \

9. What are the gcc flags you know?

10. What are the gcc extensions you know?