InterviewSkill

C Interview Questions

C fundamentals for memory, pointers, compilation, and low-level programming interview rounds.

8 questions
C

What is a pointer in C?easy

Type
conceptual
Topic
pointers
Frequency
common
Tags
pointers, memory
Answer

A pointer is a variable that stores the memory address of another value.

Explanation

Pointers let C programs access and modify memory directly. They are used for dynamic memory, arrays, strings, pass-by-reference style APIs, and low-level system programming.

Follow-upWhat is the difference between int *p and *p?

How are arrays and pointers related in C?medium

Type
conceptual
Topic
array-pointer
Frequency
common
Tags
arrays, pointers
Answer

In many expressions, an array decays into a pointer to its first element, but an array and a pointer are not the same type.

Explanation

An array owns a fixed block of elements, while a pointer stores an address. Array decay explains why arr[i] and *(arr + i) work similarly, but sizeof(arr) and sizeof(pointer) behave differently.

Follow-upWhy does sizeof behave differently for arrays and pointers?

How are strings represented in C?easy

Type
conceptual
Topic
strings
Frequency
common
Tags
strings, char-array
Answer

C strings are arrays of char terminated by a null character, '\0'.

Explanation

C does not have a built-in string object. String functions scan memory until they find the null terminator, so missing terminators can cause buffer overreads and undefined behavior.

Follow-upWhy can strlen be O(n)?

What do malloc and free do?medium

Type
conceptual
Topic
malloc-free
Frequency
common
Tags
malloc, free, heap
Answer

malloc allocates memory on the heap, and free releases heap memory that is no longer needed.

Explanation

malloc returns a pointer to a requested number of bytes. The programmer must check for NULL, use the memory within bounds, and free it exactly once to avoid leaks, crashes, or double-free bugs.

Follow-upWhat happens if you use memory after calling free?

What is the difference between stack and heap memory?medium

Type
conceptual
Topic
stack-heap
Frequency
common
Tags
stack, heap, memory
Answer

Stack memory is automatically managed for function calls, while heap memory is manually managed for dynamic allocation.

Explanation

Local variables usually live on the stack and disappear when the function returns. Heap memory can outlive a function call, but the program must free it to avoid leaks.

Follow-upWhy is returning a pointer to a local variable dangerous?

What is a struct in C?easy

Type
conceptual
Topic
structs
Frequency
common
Tags
struct, alignment
Answer

A struct groups multiple fields under one user-defined type.

Explanation

Structs are used to model records such as nodes, coordinates, records, and configuration objects. Their memory layout may include padding for alignment.

Follow-upWhy can a struct be larger than the sum of its fields?

Why do C programs use header files?medium

Type
conceptual
Topic
header-files
Frequency
common
Tags
headers, compilation
Answer

Header files share declarations such as function prototypes, macros, and type definitions across source files.

Explanation

A .h file describes an interface, while .c files usually contain implementations. Include guards or #pragma once prevent duplicate declarations during preprocessing.

Follow-upWhat is the difference between declaration and definition?

What is undefined behavior in C?hard

Type
conceptual
Topic
undefined-behavior
Frequency
common
Tags
undefined-behavior, memory-safety
Answer

Undefined behavior means the C standard gives no guarantee about what the program will do.

Explanation

Examples include out-of-bounds access, signed integer overflow, use-after-free, and dereferencing invalid pointers. Compilers may optimize assuming undefined behavior never happens.

Follow-upWhy can undefined behavior make bugs hard to reproduce?