C programming interview questions

Pointers, memory, storage classes, structures and the classic C viva questions.

1. What is the difference between C and C++?

C is a procedural language; C++ adds object-oriented features (classes, inheritance, polymorphism), references, function overloading, templates, exceptions and the STL. C++ is mostly a superset of C.

Asked in: Capgemini

2. What is a pointer?

A variable that stores the address of another variable. You get an address with & and read the value at an address with *. Pointers allow dynamic memory, efficient array handling and passing variables by reference.

Asked in: Capgemini

3. What is the difference between malloc and calloc? Static versus dynamic memory?

Static memory is fixed at compile time (normal arrays, globals). Dynamic memory is requested at run time from the heap. malloc(size) allocates a block without initialising it; calloc(n, size) allocates n elements and sets them to zero. Both return NULL on failure and must be released with free().

Asked in: Wipro Elite NTH

4. What are dangling, null and wild pointers?

A null pointer points nowhere (NULL). A wild pointer is declared but never initialised. A dangling pointer still holds the address of memory that has been freed or has gone out of scope. Set pointers to NULL after free to avoid using them.

5. What is a memory leak?

Dynamic memory that is allocated but never freed, so it cannot be used again while the program runs. Every malloc/calloc should have a matching free.

6. What is call by value versus call by reference?

Call by value passes a copy, so changes inside the function do not affect the caller. Call by reference passes the address (a pointer in C), so the function can change the original variable, as in swap(&a, &b).

7. What are the storage classes in C?

auto (default for local variables), register (a hint to keep it in a CPU register), static (keeps its value between function calls, or limits a global to one file) and extern (declares a variable defined in another file).

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

In a structure every member has its own memory, so the size is at least the sum of the members. In a union all members share the same memory, so the size equals the largest member and only one member holds a valid value at a time.

9. What is the difference between float and double?

Both store decimal numbers. float usually takes 4 bytes with about 7 digits of precision; double takes 8 bytes with about 15–16 digits. Use double when accuracy matters.

Asked in: TCS NQT

10. What is the difference between a macro and a function?

A macro (#define) is replaced by the preprocessor before compilation, so there is no function-call cost but also no type checking. A function is compiled once and called at run time with type-checked arguments.

11. What is the difference between #include <file> and #include "file"?

Angle brackets search the standard system include folders (for library headers like stdio.h). Quotes search the current project folder first, then the system folders (for your own headers).

12. What is recursion?

A function calling itself with a smaller input until it reaches a base case, such as factorial(n) = n × factorial(n − 1) with factorial(0) = 1. Without a base case it never stops and overflows the stack.

Asked in: Wipro Elite NTH

All interview preparation