C Interview Questions and Answers for Freshers (Part 4)

Mou

October 29, 2025


C Interview Questions and Answers for Freshers (Part 4)

If you’re preparing for your first C programming interview, this guide will help you cover the next set of commonly asked questions — from memory management to string handling and file operations. Let’s go step-by-step.


1. What is recursion in C? Give an example.

Answer:
Recursion is when a function calls itself to solve a smaller part of a bigger problem. It continues calling itself until a base condition is met.

Example:

#include <stdio.h>
int factorial(int n) {
    if (n == 0)
        return 1; // base case
    else
        return n * factorial(n - 1);
}
int main() {
    printf("Factorial of 5 is %d", factorial(5));
    return 0;
}

Explanation:
Here, factorial() keeps calling itself with smaller values until it reaches 0.


2. What is the difference between call by value and call by reference?

Answer:

  • Call by Value: A copy of the variable is passed to the function. Changes inside the function don’t affect the original variable.
  • Call by Reference: The address of the variable is passed, so changes inside the function directly modify the original value.

Example:

void changeValue(int x) { x = 10; } // call by value
void changeRef(int *x) { *x = 10; } // call by reference

3. What are pointers in C?

Answer:
A pointer is a variable that stores the memory address of another variable.

Example:

int a = 5;
int *ptr = &a;
printf("Value: %d", *ptr);

Here, ptr stores the address of a, and *ptr accesses its value.


4. What is NULL pointer?

Answer:
A NULL pointer doesn’t point to any valid memory location. It’s used for safety to avoid accessing garbage memory.

Example:

int *ptr = NULL;
if (ptr == NULL)
    printf("Pointer is empty");

5. What is dangling pointer?

Answer:
A dangling pointer points to memory that has already been freed or deleted.

Example:

int *ptr = (int*)malloc(sizeof(int));
free(ptr);
printf("%d", *ptr); // dangling pointer - undefined behavior

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

Answer:

FunctionInitializationUsage
malloc()Doesn’t initialize memoryint *a = (int*)malloc(5*sizeof(int));
calloc()Initializes with 0int *a = (int*)calloc(5, sizeof(int));

Both allocate dynamic memory, but calloc() also sets the allocated memory to zero.


7. What is the purpose of the free() function?

Answer:
The free() function releases memory that was allocated using malloc() or calloc().

Example:

int *a = malloc(10 * sizeof(int));
free(a); // prevent memory leak

8. What is memory leak in C?

Answer:
A memory leak happens when dynamically allocated memory is not freed. It stays reserved and cannot be used again, wasting memory.

Example:

void test() {
    int *p = malloc(10 * sizeof(int));
    // forgot to free(p)
}

9. What is an array in C?

Answer:
An array is a collection of elements of the same type stored in continuous memory locations.

Example:

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

10. What is the difference between array and pointer?

Answer:

  • An array holds multiple values of the same type.
  • A pointer holds the address of a variable or an array.

Example:

int arr[3] = {1, 2, 3};
int *ptr = arr;
printf("%d", *(ptr+1)); // prints 2

11. What is a string in C?

Answer:
A string is a sequence of characters terminated by a null character '\0'.

Example:

char name[] = "Glimsy";
printf("%s", name);

12. What is the difference between strcmp() and strcpy()?

Answer:

FunctionPurpose
strcmp(str1, str2)Compares two strings
strcpy(dest, src)Copies one string to another

Example:

char s1[10] = "Cat";
char s2[10] = "Dog";
printf("%d", strcmp(s1, s2)); // non-zero result

13. What is structure in C?

Answer:
A structure is a user-defined data type that groups different data types together.

Example:

struct Student {
    int id;
    char name[20];
};

14. How to access structure members?

Answer: Use the dot (.) operator or arrow (->) operator if using pointers.

Example:

struct Student s1 = {1, "Amit"};
printf("%d %s", s1.id, s1.name);

15. What is the difference between structure and union?

Answer:

FeatureStructureUnion
MemorySeparate memory for each memberShared memory for all members
UsageWhen all members are neededWhen one member is used at a time

16. What is typedef in C?

Answer:
typedef gives a new name to an existing data type.

Example:

typedef unsigned int uint;
uint age = 25;

17. What is an enum in C?

Answer:
An enum is a user-defined type that gives names to integer constants.

Example:

enum week {Mon, Tue, Wed, Thu, Fri, Sat, Sun};
enum week day = Wed;
printf("%d", day); // prints 2

18. What is file handling in C?

Answer:
File handling lets you read and write data to files using functions like fopen(), fprintf(), fscanf(), fclose() etc.

Example:

FILE *fp = fopen("data.txt", "w");
fprintf(fp, "Hello C!");
fclose(fp);

19. Difference between text and binary files?

Answer:

File TypeDescription
Text FileContains readable characters (like .txt)
Binary FileContains raw data (like .dat, .bin)

20. What is the purpose of fopen() and fclose()?

Answer:

  • fopen() – Opens a file for reading/writing.
  • fclose() – Closes the opened file.

Example:

FILE *f = fopen("glimsy.txt", "r");
fclose(f);

21. What are command-line arguments?

Answer:
They allow users to pass arguments to the program while executing it.

Example:

#include <stdio.h>
int main(int argc, char *argv[]) {
    printf("Program name: %s", argv[0]);
    return 0;
}

22. What is preprocessor in C?

Answer:
The preprocessor runs before compilation and handles directives like #include, #define, etc.


23. What is the difference between #include <file.h> and #include “file.h”?

Answer:

  • #include <file.h> → Searches in system directories.
  • #include "file.h" → Searches in current directory first.

24. What is macro in C?

Answer:
A macro is a piece of code that gets replaced by its value before compilation.

Example:

#define PI 3.14
printf("%f", PI);

25. What is the difference between macro and function?

Answer:

MacroFunction
Processed by preprocessorExecuted at runtime
No type checkingType checking done
Faster but less safeSlower but safer

26. What is the purpose of static keyword?

Answer:

  • In functions: makes them accessible only inside the same file.
  • In variables: retains value between function calls.

Example:

void count() {
    static int x = 0;
    x++;
    printf("%d ", x);
}

27. What is the use of const keyword?

Answer:
It makes a variable read-only — its value can’t be changed after initialization.

Example:

const int MAX = 100;

28. What is segmentation fault?

Answer:
A segmentation fault occurs when a program tries to access restricted or invalid memory.

Example: Dereferencing a NULL pointer can cause it.


29. What is the purpose of exit() function?

Answer:
It immediately terminates the program and returns a status code.

Example:

if (file == NULL) {
    printf("File not found!");
    exit(1);
}

30. What are header files?

Answer:
Header files contain function declarations and macros.
Example:
#include <stdio.h> or #include "myheader.h"


🔸 Final Tips for Freshers

  • Practice coding small programs like factorial, palindrome, swapping, etc.
  • Understand memory, loops, and pointers deeply — they’re common in interviews.
  • Always free dynamically allocated memory.
  • Be confident to explain your logic step-by-step.


Leave a Comment