HOME HTML EDITOR C JAVA PHP

C Interview questions and answers

C interviews often focus on memory layout, pointer manipulation, and hardware interaction. Unlike high-level language interviews, C interviews test your understanding of what happens "under the hood."

1. Fundamental Concepts

Q1: What is the difference between #include <file.h> and #include "file.h"?

The difference lies in the search path the preprocessor follows:

Q2: What are the different Storage Classes in C?

Storage classes define the scope, default value, and lifetime of a variable.

Class Storage Lifetime
auto Stack Function block
static Data Segment Program duration
extern Data Segment Program duration
register CPU Register Function block

2. Pointers and Memory Management

Q3: What is a Dangling Pointer and how is it different from a Wild Pointer?

Both are dangerous but have different origins:

Q4: Explain the difference between malloc() and calloc().

While both allocate memory on the Heap, they differ in initialization:

3. Advanced Syntax & Keywords

Q5: What is the volatile keyword used for?

The volatile keyword tells the compiler not to optimize the variable because its value can change unexpectedly outside the control of the program (e.g., a hardware register, an interrupt service routine, or a variable shared by multiple threads). Without volatile, the compiler might assume the value hasn't changed and read a "stale" value from a CPU register instead of RAM.

Q6: Can a struct contain a pointer to itself?

Yes, this is called a Self-Referential Structure. It is the basis for linked lists, trees, and graphs.

struct Node {
    int data;
    struct Node *next; // Pointer to another struct of the same type
};

4. Common Pitfalls & Debugging

Q7: What is a Memory Leak? How do you prevent it?

A memory leak occurs when a programmer allocates memory on the heap (using malloc/calloc) but fails to release it using free() after it is no longer needed. Over time, the program consumes more and more RAM until it crashes or slows down the system.
Prevention: Always pair every allocation with a free() and use tools like Valgrind to detect leaks during development.

Q8: What is the difference between ++i and i++?

5. Tricky Logical Questions

Q9: How can you swap two numbers without using a temporary variable?

You can use the Bitwise XOR operator to perform the swap efficiently:

a = a ^ b;
b = a ^ b;
a = a ^ b;

Q10: What is the purpose of the const qualifier?

The const keyword ensures that a variable's value cannot be modified after initialization. However, in C, you must be careful with pointers:

Interview Pro Tip: When answering technical questions, don't just explain the "what"—explain the "why." For example, instead of just saying malloc doesn't initialize memory, explain that it's designed that way for maximum performance in high-speed applications.