🚀 We're LIVE on Product Hunt today — come say hi & support the launch →
All interview questions Programming · 2026

C Programming Interview Questions

C is a core topic in systems, embedded, and backend interviews, and a favorite for testing fundamentals like memory and pointers. These are the questions interviewers actually ask, with concise answers you can speak confidently.

17 questions with concise, interview-ready answers.

1. What is a pointer in C?

A pointer is a variable that stores the memory address of another variable rather than a value directly. You declare one with a type and an asterisk, take an address with the & operator, and read or write the value it points to by dereferencing it with the * operator. Pointers are central to C because they enable dynamic memory, efficient array and string handling, and passing data to functions by reference.

2. What is the difference between malloc and calloc?

Both allocate memory on the heap and return a pointer to it. malloc takes a single size in bytes and leaves the memory uninitialized, so it may contain garbage. calloc takes a count and an element size, allocates that total, and initializes every byte to zero. Both return NULL if the allocation fails, and memory from either must be released with free.

3. What does the free function do, and what happens if you misuse it?

free releases a block of heap memory that was previously allocated by malloc, calloc, or realloc so it can be reused. You should pass it the exact pointer you received from the allocator. Freeing the same pointer twice (a double free) or freeing memory that was not dynamically allocated causes undefined behavior, and using a pointer after freeing it leads to a dangling-pointer bug.

4. What is the difference between an array and a pointer?

An array is a contiguous block of elements whose name represents a fixed location, while a pointer is a separate variable that can hold and change an address. An array name decays to a pointer to its first element in most expressions, which is why they look similar, but you cannot reassign an array name and sizeof on an array gives the total bytes whereas sizeof on a pointer gives just the pointer size. They are related but not interchangeable.

5. What is the difference between call by value and call by reference?

In call by value the function receives a copy of the argument, so changes inside the function do not affect the caller's original variable. C is always call by value, but you simulate call by reference by passing a pointer: the pointer itself is copied, yet it still points to the caller's variable, so dereferencing it lets the function modify the original. This is how functions like scanf or a swap routine change a caller's data.

6. What is the difference between a structure and a union?

A structure groups several members and gives each its own memory, so its total size is roughly the sum of all members plus any padding, and all members can hold values at the same time. A union shares one block of memory among all its members, so its size is that of its largest member and only one member holds a valid value at any moment. Use a structure to store related data together and a union to save space when only one of several fields is needed at a time.

7. What are the storage classes in C?

C has four storage classes that set a variable's lifetime, scope, and linkage. auto is the default for local variables, which live only during their block. static gives a variable a lifetime that lasts the whole program while limiting its visibility. extern declares a variable or function defined in another file so it can be shared across translation units. register is a hint to keep a variable in a CPU register for speed, though modern compilers usually ignore it.

8. What does the static keyword do in C?

static has two related uses. On a local variable it preserves the value between function calls and gives it program-long lifetime instead of being recreated each call. On a global variable or function it limits visibility to the current source file, giving it internal linkage so other files cannot see or link to it. In both cases the underlying idea is controlling lifetime and limiting scope.

9. What is a dangling pointer?

A dangling pointer is a pointer that still holds an address whose memory is no longer valid. This typically happens after you free the memory it points to, or when it points to a local variable that has gone out of scope. Dereferencing a dangling pointer is undefined behavior, so a common safeguard is to set the pointer to NULL right after freeing it.

10. What is a memory leak, and how do you avoid it?

A memory leak occurs when a program allocates heap memory but loses every reference to it without freeing it, so that memory stays reserved until the program ends. Over time leaks grow memory usage and can crash long-running programs. You avoid them by pairing every malloc or calloc with a matching free, freeing memory on all exit paths, and using tools like Valgrind to detect blocks that were never released.

11. What is the difference between the stack and the heap?

The stack stores local variables and function call frames; it is fast, managed automatically, and freed when a function returns, but it is limited in size. The heap is a larger pool of memory you allocate and free manually with malloc and free, with a longer lifetime that you control. Stack overflow happens from too-deep recursion or huge locals, while the heap can fragment and must be cleaned up by hand to avoid leaks.

12. What are header files and the role of the preprocessor?

Header files, included with #include, hold declarations such as function prototypes, macros, and type definitions that are shared across source files. The preprocessor runs before compilation and handles directives that begin with #, such as #include to paste in files, #define to create macros and constants, and #ifdef or #ifndef for conditional compilation. Include guards using #ifndef or #pragma once prevent a header from being included more than once.

13. What does the const keyword mean in C?

const marks a variable as read-only, so the compiler rejects attempts to modify it after initialization. With pointers the placement matters: a pointer to const data cannot change the data it points to, while a const pointer cannot be made to point elsewhere. It is commonly used on function parameters to signal that the function will not alter the data passed in.

14. How do you pass an array to a function in C?

When you pass an array to a function, the array name decays to a pointer to its first element, so the function receives an address rather than a copy of all the elements. Because of this the function can modify the original array, and it does not know the length on its own. The usual practice is to pass the size as a separate argument so the function knows how many elements to process.

15. How are strings handled in C?

A C string is an array of characters terminated by a null character, the \0, which marks the end so functions know where the string stops. There is no dedicated string type, so you work with char arrays or char pointers and use library functions from string.h such as strlen, strcpy, strcmp, and strcat. You must ensure the buffer is large enough including room for the null terminator, and safer variants like strncpy help avoid buffer overflows.

16. What is recursion, and what does a recursive function need?

Recursion is when a function calls itself to solve a problem by breaking it into smaller subproblems. Every recursive function needs a base case that stops the recursion and a recursive case that moves toward that base case. Each call uses a new stack frame, so without a correct base case or with too much depth you can run out of stack space and get a stack overflow.

17. What commonly causes a segmentation fault in C?

A segmentation fault happens when a program accesses memory it is not allowed to touch. Common causes include dereferencing a NULL or uninitialized pointer, using a dangling pointer after free, writing past the bounds of an array, and overflowing the stack with unbounded recursion. The fix is careful pointer handling, checking allocations for NULL, and staying within array limits.

Get these answered live in your real interview

NostrobeAI is a real-time AI interview copilot — it hears the question and drafts a strong answer on your screen, invisible on Zoom, Meet, and Teams. One-time pricing, no subscription.

Try NostrobeAI free