Posted in

Top 30 C Programming Interview Questions and Answers for All Levels

Introduction

Prepare for your next C programming interview with these 30 carefully curated questions covering basic, intermediate, and advanced topics. This guide includes conceptual explanations, practical coding examples, and scenario-based questions suitable for freshers, candidates with 1-3 years experience, and professionals with 3-6 years in C development at companies like Zoho, Atlassian, and Paytm.

Basic Level C Interview Questions (1-10)

1. What is the difference between #include <stdio.h> and #include "stdio.h"?

The angle brackets <> tell the preprocessor to search in system directories first, while double quotes "" search in the current directory first, then system directories. Use <> for standard headers and "" for custom headers.

2. Can you compile a C program without a main() function?

Yes, a C program can compile without main(), but it cannot execute since execution starts from main(). The linker will report an undefined reference error during execution.

3. What are the differences between malloc() and calloc()?

malloc() allocates uninitialized memory of specified size, while calloc() allocates memory for an array and initializes all bytes to zero. Both return NULL on failure.

#include <stdio.h>
#include <stdlib.h>

int main() {
    int *p1 = malloc(5 * sizeof(int));  // Uninitialized
    int *p2 = calloc(5, sizeof(int));   // Zero-initialized
    free(p1);
    free(p2);
    return 0;
}

4. What is a pointer in C?

A pointer is a variable that stores the memory address of another variable. Pointers enable direct memory access and are essential for dynamic memory allocation and data structures.

5. Explain the difference between call by value and call by reference in C.

C only supports call by value, where a copy of the argument is passed. To simulate call by reference, pass pointers to achieve modification of original variables.

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

6. What is the output of this code?

int main() {
    int x = 5;
    printf("%d %d %d", x++, x, ++x);
    return 0;
}

Output: 6 7 7. The behavior is undefined due to multiple modifications of x without sequence points, but common compilers evaluate right-to-left.

7. What are local and global variables?

Local variables are declared inside functions with block scope. Global variables are declared outside functions with file scope, accessible throughout the program after declaration.

8. What is recursion in C?

Recursion is when a function calls itself to solve smaller instances of the same problem. It requires a base case to terminate. Example: factorial calculation.

int factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

9. What does the const keyword do?

const creates read-only variables that cannot be modified after initialization. It improves code safety and enables compiler optimizations.

10. What is typecasting in C?

Typecasting converts a variable from one data type to another. Implicit casting happens automatically; explicit casting uses (type) syntax.

double d = 5.7;
int i = (int)d;  // Explicit casting: i = 5

Intermediate Level C Interview Questions (11-20)

11. How do you detect a memory leak in C?

Memory leaks occur when dynamically allocated memory isn't freed. Use tools like Valgrind or track allocations with counters. Always pair malloc() with free().

12. Write a program to reverse a string in place.

#include <string.h>
void reverse(char *str) {
    int len = strlen(str);
    for (int i = 0; i < len / 2; i++) {
        char temp = str[i];
        str[i] = str[len - 1 - i];
        str[len - 1 - i] = temp;
    }
}

13. What is a segmentation fault? Give an example.

A segmentation fault occurs when accessing invalid memory. Example: dereferencing a NULL pointer causes the program to crash.

int *ptr = NULL;
*ptr = 10;  // Segmentation fault

14. Explain structures vs unions in C.

Structures store multiple members with separate memory for each. Unions share memory among members; only one member is active at a time, using the size of the largest member.

15. How do you implement binary search in C?

int binarySearch(int arr[], int n, int key) {
    int left = 0, right = n - 1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (arr[mid] == key) return mid;
        if (arr[mid] < key) left = mid + 1;
        else right = mid - 1;
    }
    return -1;
}

16. What are storage classes in C?

Storage classes control variable lifetime, visibility, and initial value: auto, register, static, extern. static retains value between calls.

17. Write a program to check if a number is prime.

int isPrime(int n) {
    if (n <= 1) return 0;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return 0;
    }
    return 1;
}

18. What is the difference between == and = operators?

= is assignment operator. == is equality comparison. Common mistake: using = in if conditions causing logical errors.

19. How do you read command line arguments in C?

Use int main(int argc, char *argv[]). argc is argument count, argv is argument array. argv[0] is program name.

int main(int argc, char *argv[]) {
    for (int i = 1; i < argc; i++) {
        printf("%s ", argv[i]);
    }
    return 0;
}

20. What are preprocessor directives?

Preprocessor directives starting with # (like #include, #define) are processed before compilation. They handle macros, conditional compilation, and file inclusion.

Advanced Level C Interview Questions (21-30)

21. What is a function pointer? Give an example.

A function pointer stores the address of a function and enables dynamic function calls. Used in callbacks and qsort comparators.

int add(int a, int b) { return a + b; }
int main() {
    int (*funcPtr)(int, int) = add;
    printf("%d", funcPtr(3, 4));  // Output: 7
}

22. Explain volatile keyword usage.

volatile tells the compiler not to optimize accesses to the variable, useful for hardware registers and multi-threaded variables that can change unexpectedly.

23. How do you copy a string without using strcpy()?

void myStrcpy(char *dest, const char *src) {
    while (*src) {
        *dest++ = *src++;
    }
    *dest = '\0';
}

24. What causes undefined behavior in C?

Undefined behavior includes dereferencing NULL, signed integer overflow, multiple modifications without sequence points, and array out-of-bounds access. Results are unpredictable.

25. Implement a stack using arrays in C.

#define MAX 100
int stack[MAX];
int top = -1;

void push(int data) {
    if (top < MAX - 1) stack[++top] = data;
}

int pop() {
    if (top >= 0) return stack[top--];
    return -1;
}

26. Difference between stack and heap memory?

Stack: Automatic allocation/deallocation, fast, fixed size. Heap: Manual allocation (malloc/free), slower, flexible size but prone to fragmentation.

27. Write a macro to find maximum of two numbers.

#define MAX(a, b) ((a) > (b) ? (a) : (b))

Macros expand textually before compilation, faster than functions but no type checking.

28. How do you handle file errors in C?

Check return values of file functions: fopen() returns NULL, fread()/fwrite() return byte counts, feof() detects end-of-file.

29. Scenario: At Zoho, you need to swap two numbers without a temporary variable. How?

void swapNoTemp(int *a, int *b) {
    *a = *a + *b;
    *b = *a - *b;
    *a = *a - *b;
}

30. Scenario: Atlassian needs a circular linked list detection algorithm. Implement it.

int isCircular(struct Node *head) {
    if (!head) return 0;
    struct Node *slow = head, *fast = head;
    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
        if (slow == fast) return 1;
    }
    return 0;
}

Key Takeaways: Master pointers, memory management, and practical coding. Practice these questions to excel in C interviews at product companies, SaaS platforms, and startups.

Leave a Reply

Your email address will not be published. Required fields are marked *