Interview Questions

C Interview Questions and Answers

  1. What is C language?

    C is a general-purpose, procedural programming language developed by Dennis Ritchie at Bell Labs in the early 1970s. It's widely used for system programming, embedded systems, and developing operating systems due to its efficiency and control over hardware.

  2. What are the main features of C language?

    The main features of C language include:

    • Simple and efficient
    • Rich set of built-in functions and operators
    • Portability
    • Pointer support
    • Structured programming language
    • Low-level access to memory
  3. Explain the difference between malloc() and calloc().

    malloc(size_t size) allocates a block of memory of a specified size but doesn't initialize it, while calloc(size_t n, size_t size) allocates memory for an array of n elements and initializes all bytes to zero.

  4. What is the difference between ++i and i++?

    ++i (pre-increment) increments the value of i before using it in an expression, while i++ (post-increment) uses the value of i in the expression and then increments it.

  5. What is a pointer in C?

    A pointer is a variable that stores the memory address of another variable. Pointers are powerful tools in C for dynamic memory allocation, arrays, and functions.

  6. Explain the concept of dynamic memory allocation in C.

    Dynamic memory allocation allows the program to allocate memory during runtime using functions like malloc(), calloc(), realloc(), and free().

  7. What is a memory leak and how can you prevent it in C?

    A memory leak occurs when dynamically allocated memory is not freed, leading to a waste of memory resources. It can be prevented by ensuring every malloc() or calloc() call has a corresponding free() call.

  8. Describe the sizeof operator.

    The sizeof operator in C returns the size (in bytes) of a data type or object.

  9. How do you reverse a linked list in C?

    This can be done by iteratively changing the next pointers of the nodes or using recursion.

  10. Explain how to implement a stack using an array in C.

    A stack can be implemented using an array where the top of the stack is managed using an index. Operations like push, pop, and peek are performed based on this index.

  11. What are function pointers and how are they used in C?

    Function pointers are pointers that point to the address of a function. They are used for callback functions, implementing function tables, and creating dynamic dispatch mechanisms.

  12. Explain the concept of bitwise operators and their use cases.

    Bitwise operators perform operations at the bit level (AND, OR, XOR, NOT, etc.). They are used in tasks that involve bit manipulation, such as setting or clearing flags, encryption, and compressing data.

  13. What are the differences between structure and union in C?

    A structure allocates separate memory for each of its members, whereas a union allocates a single memory location shared by all its members. The size of a union is determined by its largest member.

  14. What is the difference between struct and typedef struct in C?

    struct defines a structure type. Using typedef struct allows you to create an alias for the structure, so you can avoid using the struct keyword when declaring variables of that type.

  15. What is a segmentation fault?

    A segmentation fault occurs when a program attempts to access memory that it is not allowed to, often due to dereferencing a null or uninitialized pointer.

  16. How does the assert macro work?

    The assert macro is used for debugging purposes. It checks a condition and if the condition is false, it displays an error message and terminates the program.

  17. Write a C program to find the factorial of a number.

    The program should calculate the factorial of a given number using either a loop or recursion.

  18. Implement a C program to reverse a string without using built-in functions.

    The program should take a string input and reverse it by manually swapping characters.

  19. How would you find the maximum element in an array using C?

    The program should iterate through the array, keeping track of the maximum value encountered.

  20. Write a C program to check if a number is a palindrome.

    The program should check if the given integer reads the same forwards and backwards.

  21. Write a C program to find the second largest element in an array.

    The program should traverse the array, keeping track of the largest and second-largest elements.

  22. How do you remove duplicates from an array in C?

    Write a program that eliminates duplicates from an unsorted array, ideally without using extra memory for another array.

  23. Implement a function to rotate an array to the right by k positions.

    The function should modify the array in place to shift all elements k positions to the right.

  24. Write a C program to check if two strings are anagrams of each other.

    An anagram is a word formed by rearranging the letters of another word.

  25. Write a function to reverse the words in a given string.

    The input is a string, and the output should be the string with words in reverse order.

  26. Implement a function to detect a cycle in a linked list.

    Use Floyd’s Cycle detection algorithm (tortoise and hare) to determine if a cycle exists.

  27. Write a C function to merge two sorted linked lists into one sorted linked list.

    The function should take two pointers to the heads of the lists and return a pointer to the head of the merged list.

  28. How do you find the middle element of a linked list in a single pass?

    Use two pointers, one moving twice as fast as the other. When the fast pointer reaches the end, the slow pointer will be at the middle.

  29. Write a C program to count the number of 1s in the binary representation of an integer.

    The program should efficiently count the bits set to 1 in the integer's binary form.

  30. How do you swap two numbers without using a temporary variable in C?

    Implement this using bitwise XOR.

Creative Footer for Interview Questions