Posted in

Top 30 C Programming Interview Questions and Answers for All Levels

Prepare for your next C programming interview with these 30 essential questions covering basic, intermediate, and advanced topics. Whether you’re a fresher, have 1-3 years of experience, or are a seasoned professional with 3-6 years, this guide helps you master core C concepts, pointers, memory management, and practical coding scenarios.

Basic C Interview Questions (1-10)

1. What is C and why is it called a middle-level language?

C is a procedural programming language that combines low-level memory access with high-level abstractions, allowing direct hardware manipulation while providing structured programming features.[1][6]

2. What are the basic data types in C?

C supports primary data types: int, char, float, double, and void. Derived types include arrays, pointers, structures, and unions.[3][5]

3. What is the difference between local and global variables?

Local variables are declared inside a function and have block scope, while global variables are declared outside functions and are accessible throughout the program.[5]

4. Explain the structure of a basic C program.

A C program starts with preprocessor directives (#include), followed by global declarations, the main() function containing declarations, statements, and sub-functions.[5]

#include <stdio.h>
int main() {
    printf("Hello, World!\n");
    return 0;
}

5. What is the purpose of the main() function?

main() is the entry point of every C program. Execution begins here, and it returns an integer value (0 for success) to the operating system.[5]

6. Differentiate between == and = operators.

= is the assignment operator that stores a value, while == is the equality operator that compares two values for equality.[1]

7. What are constants in C? How do you declare them?

Constants are fixed values that cannot change. Use the const keyword: const int MAX = 100;[3]

8. What is a preprocessor directive?

Preprocessor directives like #include, #define, and #ifdef are processed before compilation to include files, define macros, and handle conditional compilation.[2][3]

9. What does #include <stdio.h> do?

It includes the standard input/output header file, providing functions like printf(), scanf(), getchar(), and putchar().[5]

10. What is the difference between break and continue?

break exits the loop entirely, while continue skips the current iteration and proceeds to the next.[3]

Intermediate C Interview Questions (11-20)

11. What are arrays in C? How do you declare a 2D array?

Arrays store multiple values of the same type. A 2D array declaration: int matrix[3][4]; represents 3 rows and 4 columns.[1]

int arr[5] = {1, 2, 3, 4, 5};
printf("%d", arr[0]); // Outputs 1

12. Explain strings in C.

Strings are character arrays ending with ‘\0’. Declare as: char str[50] = "Hello";[1]

13. What is a pointer? Write a simple pointer example.

A pointer stores the memory address of another variable. Use * for declaration and & for address.[1][3]

int x = 10;
int *ptr = &x;
printf("%d", *ptr); // Outputs 10 (dereference)

14. What is the difference between malloc() and calloc()?

malloc() allocates uninitialized memory, while calloc() allocates memory initialized to zero.[3]

15. How do you reverse a string in C at Flipkart?

Use two pointers or a temporary array to swap characters from start and end until they meet.

#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;
    }
}

16. What are storage class specifiers?

auto, register, static, extern control variable scope, lifetime, and storage location.[1]

17. Explain recursion with an example for factorial.

Recursion is a function calling itself. Base case prevents infinite calls.[3]

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

18. What is typecasting? Provide an example.

Typecasting converts one data type to another: int num = (int)3.14;[3]

19. How do you find the size of a variable using sizeof?

sizeof operator returns the memory size: sizeof(int) typically returns 4 bytes.[2]

20. Implement strlen() without using library functions for Zoho interviews.

Iterate through characters until ‘\0’ is encountered.

int my_strlen(char *str) {
    int len = 0;
    while(str[len] != '\0') len++;
    return len;
}

Advanced C Interview Questions (21-30)

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

Function pointer stores the address of a function: int (*fptr)(int) = myfunc;[1]

int add(int a, int b) { return a+b; }
int main() {
    int (*op)(int,int) = add;
    printf("%d", op(2,3)); // 5
}

22. Explain structure padding and how to avoid it.

Padding adds unused bytes for alignment. Use #pragma pack(1) or reorder members.[1]

23. What are bit fields in structures?

Bit fields allocate specific bits: struct { int flag:1; int value:7; };[3]

24. Differentiate stack and heap memory.

Stack is automatic and fast but limited; heap is dynamic via malloc/free but manual management.[2]

25. Write a program to count set bits in Paytm scenarios.

Use bitwise AND with shifting or Brian Kernighan’s algorithm.[2]

int count_set_bits(int n) {
    int count = 0;
    while(n) {
        n &= (n-1);
        count++;
    }
    return count;
}

26. What is a memory leak and how to prevent it?

Memory leak occurs when allocated memory isn’t freed. Always pair malloc with free.[1]

27. Implement binary search for Salesforce coding rounds.

Divide sorted array mid-point repeatedly.

int binary_search(int arr[], int size, int key) {
    int left = 0, right = size-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;
}

28. What is the volatile keyword?

volatile prevents compiler optimization for variables that may change unexpectedly, like hardware registers.[1]

29. Explain lvalue and rvalue for Oracle technical rounds.

lvalue has address (assignable), rvalue is a value (not assignable).[1]

30. How do you handle file operations in C for SAP applications?

Use fopen(), fread(), fwrite(), fclose(). Check file pointers for NULL.[1]

FILE *fp = fopen("file.txt", "r");
if(fp == NULL) { /* error */ }
fclose(fp);

Leave a Reply

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