C Interview Questions

C Interview Questions – Prepare for C Programming Interviews

In this tutorial, we will cover some of the most commonly asked C programming interview questions. Whether you are a beginner or an experienced programmer, these questions will help you brush up on key concepts and prepare for your next interview.

🔹 Question 1: What is the difference between C and C++?

Answer: C is a procedural programming language, while C++ is an object-oriented programming language. C focuses on functions and procedures, whereas C++ supports classes and objects for data abstraction, encapsulation, inheritance, and polymorphism.

🔹 Question 2: What is a pointer in C? How does it work?

Answer: A pointer is a variable that stores the memory address of another variable. In C, pointers are used to dynamically allocate memory and pass values by reference. To declare a pointer, we use the `*` symbol.

📝 Example: Pointer Usage

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
int main() {
int num = 10;
int *ptr = &num; // Pointer to num
printf("Value of num: %d\n", *ptr); // Dereferencing pointer to get value
return 0;
}
#include <stdio.h> int main() { int num = 10; int *ptr = &num; // Pointer to num printf("Value of num: %d\n", *ptr); // Dereferencing pointer to get value return 0; }
#include <stdio.h>

int main() {
    int num = 10;
    int *ptr = &num;  // Pointer to num
    
    printf("Value of num: %d\n", *ptr);  // Dereferencing pointer to get value
    
    return 0;
}

Try It Now

🔹 Question 3: What is the use of the `static` keyword in C?

Answer: The `static` keyword in C is used to maintain the value of a variable between function calls. It can be used for both local and global variables. When applied to a local variable, it preserves its value even after the function has returned.

🔹 Question 4: What is the difference between `malloc()` and `calloc()`?

Answer: Both `malloc()` and `calloc()` are used to dynamically allocate memory. The difference is that `malloc()` allocates a block of memory of a given size without initializing it, while `calloc()` allocates memory for an array of elements and initializes all bytes to zero.

📝 Example: Using `malloc()` and `calloc()`

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr_malloc, *arr_calloc;
int i;
// Using malloc
arr_malloc = (int *)malloc(5 * sizeof(int));
for (i = 0; i < 5; i++) {
arr_malloc[i] = i + 1; // Initialize manually
}
// Using calloc
arr_calloc = (int *)calloc(5, sizeof(int)); // Automatically initialized to 0
printf("Array allocated with malloc: ");
for (i = 0; i < 5; i++) {
printf("%d ", arr_malloc[i]);
}
printf("\n");
printf("Array allocated with calloc: ");
for (i = 0; i < 5; i++) {
printf("%d ", arr_calloc[i]);
}
printf("\n");
free(arr_malloc); // Don't forget to free the memory
free(arr_calloc); // Don't forget to free the memory
return 0;
}
#include <stdio.h> #include <stdlib.h> int main() { int *arr_malloc, *arr_calloc; int i; // Using malloc arr_malloc = (int *)malloc(5 * sizeof(int)); for (i = 0; i < 5; i++) { arr_malloc[i] = i + 1; // Initialize manually } // Using calloc arr_calloc = (int *)calloc(5, sizeof(int)); // Automatically initialized to 0 printf("Array allocated with malloc: "); for (i = 0; i < 5; i++) { printf("%d ", arr_malloc[i]); } printf("\n"); printf("Array allocated with calloc: "); for (i = 0; i < 5; i++) { printf("%d ", arr_calloc[i]); } printf("\n"); free(arr_malloc); // Don't forget to free the memory free(arr_calloc); // Don't forget to free the memory return 0; }
#include <stdio.h>
#include <stdlib.h>

int main() {
    int *arr_malloc, *arr_calloc;
    int i;
    
    // Using malloc
    arr_malloc = (int *)malloc(5 * sizeof(int));
    for (i = 0; i < 5; i++) {
        arr_malloc[i] = i + 1;  // Initialize manually
    }

    // Using calloc
    arr_calloc = (int *)calloc(5, sizeof(int));  // Automatically initialized to 0
    
    printf("Array allocated with malloc: ");
    for (i = 0; i < 5; i++) {
        printf("%d ", arr_malloc[i]);
    }
    printf("\n");
    
    printf("Array allocated with calloc: ");
    for (i = 0; i < 5; i++) {
        printf("%d ", arr_calloc[i]);
    }
    printf("\n");

    free(arr_malloc);  // Don't forget to free the memory
    free(arr_calloc);  // Don't forget to free the memory
    
    return 0;
}

Try It Now

🔹 Question 5: Explain the concept of a memory leak in C.

Answer: A memory leak occurs when a program allocates memory dynamically (using `malloc()` or `calloc()`), but fails to release it (using `free()`) after it is no longer needed. This causes the program to consume more and more memory, which can eventually lead to a crash or slowdown.

🔹 Question 6: What is the difference between `==` and `=` in C?

Answer: `==` is the equality operator, used to compare two values, while `=` is the assignment operator, used to assign a value to a variable.

📝 Example: `==` vs `=`

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
int main() {
int x = 5;
int y = 10;
if (x == y) {
printf("x and y are equal.\n");
} else {
printf("x and y are not equal.\n");
}
x = 20; // Assigning a new value to x
printf("New value of x: %d\n", x);
return 0;
}
#include <stdio.h> int main() { int x = 5; int y = 10; if (x == y) { printf("x and y are equal.\n"); } else { printf("x and y are not equal.\n"); } x = 20; // Assigning a new value to x printf("New value of x: %d\n", x); return 0; }
#include <stdio.h>

int main() {
    int x = 5;
    int y = 10;
    
    if (x == y) {
        printf("x and y are equal.\n");
    } else {
        printf("x and y are not equal.\n");
    }
    
    x = 20;  // Assigning a new value to x
    
    printf("New value of x: %d\n", x);
    
    return 0;
}

Try It Now

🔹 Question 7: What is the purpose of `#define` in C?

Answer: The `#define` directive is used to define constants and macros in C. It allows you to create symbolic names for constant values, making the code more readable and easier to maintain.

🔹 Question 8: What is the difference between `struct` and `union`?

Answer: A `struct` is a user-defined data type in C that allows grouping of different types of variables. Each member of a `struct` has its own memory location. On the other hand, a `union` is similar to a `struct` but allows storing different data types in the same memory location. Only one member of a `union` can hold a value at any given time.

🔹 Question 9: What are function pointers in C?

Answer: A function pointer is a pointer that points to the address of a function. It allows functions to be passed as arguments, enabling dynamic behavior.

📝 Example: Function Pointer

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
void say_hello() {
printf("Hello, World!\n");
}
int main() {
void (*function_ptr)() = &say_hello; // Function pointer
// Calling function using pointer
function_ptr();
return 0;
}
#include <stdio.h> void say_hello() { printf("Hello, World!\n"); } int main() { void (*function_ptr)() = &say_hello; // Function pointer // Calling function using pointer function_ptr(); return 0; }
#include <stdio.h>

void say_hello() {
    printf("Hello, World!\n");
}

int main() {
    void (*function_ptr)() = &say_hello;  // Function pointer
    
    // Calling function using pointer
    function_ptr();
    
    return 0;
}

Try It Now

🔹 Question 10: What is the use of `void` pointer in C?

Answer: A `void` pointer is a special type of pointer in C that can point to any data type. It is used for creating generic functions, where the type of the data is unknown at the time of writing the function.

🔹 Conclusion

These are some of the most frequently asked C programming interview questions. Prepare well by practicing these concepts and examples, and you’ll be ready for your next C programming interview!