C Interview Questions and Answers for 10 years experience

100 C Interview Questions and Answers (10 Years Experience)
  1. What is the difference between a static and a dynamic memory allocation?

    • Answer: Static memory allocation happens at compile time, allocating memory from the stack. It's fast but has fixed size determined at compile time. Dynamic memory allocation happens at runtime, allocating memory from the heap using functions like `malloc`, `calloc`, and `realloc`. It's flexible in size but slower and requires manual deallocation using `free` to prevent memory leaks.
  2. Explain the concept of pointers in C.

    • Answer: Pointers are variables that store memory addresses. They allow direct manipulation of memory locations. Declaring a pointer involves using the asterisk (*) symbol (e.g., `int *ptr;`). Pointers are crucial for dynamic memory allocation, data structures, and efficient memory management.
  3. What are the different storage classes in C?

    • Answer: C offers several storage classes: `auto` (default, local scope), `register` (suggests storing in a register), `static` (retains value between function calls, local or global), `extern` (declares a variable defined elsewhere), and `typedef` (creates an alias for an existing type).
  4. How do you handle memory leaks in C?

    • Answer: Memory leaks occur when dynamically allocated memory is not freed using `free()`. Careful coding practices are crucial. Always `free()` allocated memory when it's no longer needed. Using tools like Valgrind can help detect memory leaks during development.
  5. Explain the difference between `malloc` and `calloc`.

    • Answer: Both `malloc` and `calloc` dynamically allocate memory. `malloc` takes a single argument (size in bytes) and returns a void pointer. `calloc` takes two arguments (number of elements and size of each element) and initializes the allocated memory to zero. `calloc` provides the added benefit of zero initialization.
  6. What is a function pointer in C?

    • Answer: A function pointer is a pointer that stores the address of a function. It allows passing functions as arguments to other functions, enabling flexible and reusable code. Declaration involves specifying the return type and parameter types of the function being pointed to.
  7. What is the difference between `#include ` and `#include "stdio.h"`?

    • Answer: `#include ` searches for the header file in standard system directories. `#include "stdio.h"` searches for the header file in the current directory first, then in standard system directories. This distinction is important for managing project-specific header files.
  8. Explain the concept of structures in C.

    • Answer: Structures group related data elements of different data types under a single name. They are defined using the `struct` keyword. Structures enhance code organization and readability, particularly when dealing with complex data.
  9. What are unions in C and how do they differ from structures?

    • Answer: Unions, like structures, group data elements. However, all members of a union share the same memory location. Structures allocate memory for each member separately. Unions are useful when only one member is used at a time, saving memory.
  10. Explain the concept of bit fields in C structures.

    • Answer: Bit fields allow specifying the size (in bits) of structure members. This technique is useful for packing data tightly, optimizing memory usage, especially when dealing with hardware registers or embedded systems.
  11. What is a preprocessor directive? Give examples.

    • Answer: Preprocessor directives are commands that are processed before the actual compilation. They begin with a '#'. Examples include: `#include`, `#define`, `#ifdef`, `#ifndef`, `#endif`, `#if`, `#elif`, `#else`.
  12. Explain the difference between const char *ptr and char * const ptr.

    • Answer: `const char *ptr` means the pointer `ptr` points to a constant character string; the string's content cannot be changed. `char * const ptr` means the pointer itself is constant, so it cannot be changed to point to another memory location, but the content of the string it points to can be changed.
  13. What is a void pointer in C?

    • Answer: A void pointer is a generic pointer that can hold the address of any data type. It doesn't have an associated data type, so it needs to be cast to a specific type before dereferencing.
  14. How do you work with command-line arguments in C?

    • Answer: Command-line arguments are accessed through the `main` function's arguments: `int main(int argc, char *argv[])`. `argc` gives the number of arguments, and `argv` is an array of strings containing the arguments.
  15. Explain the concept of file I/O in C.

    • Answer: File I/O involves reading and writing data to files. Standard library functions like `fopen`, `fclose`, `fread`, `fwrite`, `fgets`, `fputs`, `fprintf`, and `fscanf` are used for various file operations. Error handling is crucial to manage potential issues like file not found.
  16. What are different file opening modes in C?

    • Answer: Common file opening modes include: `"r"` (read), `"w"` (write, creates if it doesn't exist, truncates if it does), `"a"` (append), `"r+"` (read and write), `"w+"` (read and write, creates and truncates), `"a+"` (read and append), `"b"` (binary mode, added to others).
  17. Explain the concept of recursion in C.

    • Answer: Recursion is a programming technique where a function calls itself. It's used to solve problems that can be broken down into smaller, self-similar subproblems. A base case is essential to prevent infinite recursion. Examples include factorial calculation and tree traversals.
  18. What is dynamic programming?

    • Answer: Dynamic programming is an algorithmic technique used to solve optimization problems by breaking them down into smaller overlapping subproblems, solving each subproblem only once, and storing their solutions to avoid redundant computations. This significantly improves efficiency compared to naive recursive approaches.
  19. Explain the difference between call by value and call by reference.

    • Answer: In call by value, a copy of the argument's value is passed to the function. Changes made to the parameter inside the function don't affect the original variable. In call by reference, the memory address of the argument is passed. Changes made inside the function affect the original variable.

Thank you for reading our blog post on 'C Interview Questions and Answers for 10 years experience'.We hope you found it informative and useful.Stay tuned for more insightful content!