Top 50 C Language Interview Questions And Answers

1. What is the C programming language?

Answer:
C is a high-level, general-purpose programming language developed by Dennis Ritchie in 1972 at Bell Labs. It is widely used for system programming, developing operating systems, embedded systems and applications due to its efficiency and control over hardware.
Example:

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

This program prints “Hello, World!” to the screen.

2. What are the main features of C language?

Answer:

  • Simplicity: Provides basic constructs and easy syntax.
  • Portability: Can run on multiple platforms with minimal changes.
  • Efficiency: High performance in terms of execution.
  • Low-level access: Direct access to memory and hardware through pointers.
  • Rich library: Standard libraries for various operations.

3. What is the difference between #include<stdio.h> and #include<conio.h>?

Answer:

  • <stdio.h>: Standard Input/Output library; functions like printf() and scanf().
  • <conio.h>: Console Input/Output functions; includes clrscr() and getch().

4. What is the difference between int and float data types?

Answer:

  • int: Stores integer values (e.g., 5, -10).
  • float: Stores floating-point numbers or decimals (e.g., 5.75, -2.3).

5. What are pointers in C?

Answer:
Pointers are variables that store the memory address of another variable. They provide direct memory manipulation.
Example:

int a = 10;
int *p = &a; // Pointer 'p' stores the address of 'a'.
printf("%d", *p); // Dereferencing gives the value of 'a'.

6. What is the purpose of main() function in C?

Answer:
The main() function is the entry point of any C program. The execution starts from here. It returns an integer value to the operating system, usually 0 to indicate successful execution.

7. What are storage classes in C?

Answer:
Storage classes define the scope, lifetime and visibility of variables. Types:

  • auto: Default for local variables.
  • static: Preserves the value of a variable across function calls.
  • extern: Declares a global variable.
  • register: Suggests storing variables in CPU registers for faster access.

8. What is the difference between while and do-while loops?

Answer:

  • while: Condition is checked before execution.
  • do-while: Executes the block at least once before checking the condition.
    Example:
int i = 1;
do {
printf("%d ", i);
i++;
} while (i <= 5); // Prints 1 2 3 4 5

9. What is a null pointer?

Answer:
A null pointer is a special pointer that does not point to any valid memory location. It is often used as an indicator that the pointer is not assigned any memory yet. Null pointers are crucial to avoid dereferencing invalid memory, which can lead to crashes.
In C, a null pointer is assigned using NULL, which is defined in <stddef.h>.

Example:

#include <stdio.h>
int main() {
int *ptr = NULL; // Pointer initialized to NULL
if (ptr == NULL) {
printf("Pointer is null and not pointing to any memory location.\n");
}
return 0;
}

10. What is recursion in C?

Answer:
Recursion is a process where a function calls itself directly or indirectly. It is often used to solve problems that can be divided into smaller, similar sub-problems. Every recursive function must have a base condition to avoid infinite recursion.

Example:
Calculating factorial using recursion:

#include <stdio.h>
int factorial(int n) {
if (n == 0) return 1; // Base condition
return n * factorial(n - 1); // Recursive call
}
int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}

Output: Factorial of 5 is 120

11. What is a structure in C?

Answer:
A structure is a user-defined data type in C that groups multiple variables of different data types under a single name. It is especially useful for organizing complex data. Structures are declared using the struct keyword.

Example:

#include <stdio.h>
struct Student {
char name[50];
int age;
float marks;
};
int main() {
struct Student s1 = {"John", 20, 85.5};
printf("Name: %s\nAge: %d\nMarks: %.2f\n", s1.name, s1.age, s1.marks);
return 0;
}

Output:

Name: John  
Age: 20
Marks: 85.50

12. What is the use of sizeof() in C?

Answer:
The sizeof() operator is used to determine the size (in bytes) of a data type or variable in memory. It is particularly useful when writing portable code that works on different architectures where the size of data types may vary.

Example:

#include <stdio.h>
int main() {
printf("Size of int: %lu bytes\n", sizeof(int));
printf("Size of float: %lu bytes\n", sizeof(float));
return 0;
}

Output (depends on architecture):

Size of int: 4 bytes  
Size of float: 4 bytes

13. What are command-line arguments in C?

Answer:
Command-line arguments allow users to pass inputs to a program at the time of its execution. The arguments are passed to the main() function through the parameters argc (argument count) and argv (argument vector).

  • argc: Stores the number of arguments passed.
  • argv: Array of character pointers representing the arguments.

Example:

#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Number of arguments: %d\n", argc);
for (int i = 0; i < argc; i++) {
printf("Argument %d: %s\n", i, argv[i]);
}
return 0;
}

If executed as ./program Hello World, the output will be:

Number of arguments: 3  
Argument 0: ./program
Argument 1: Hello
Argument 2: World

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

Answer:
Both calloc() and malloc() are used for dynamic memory allocation in C, but they have key differences:

  1. malloc(): Allocates a specified amount of memory but does not initialize it.
  2. calloc(): Allocates memory and initializes all bits to zero.

Syntax:

  • malloc: void* malloc(size_t size);
  • calloc: void* calloc(size_t num, size_t size);

Example:

#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr1 = (int*)malloc(5 * sizeof(int)); // Uninitialized memory
int *arr2 = (int*)calloc(5, sizeof(int)); // Memory initialized to 0
printf("Malloc value: %d\n", arr1[0]); // Garbage value
printf("Calloc value: %d\n", arr2[0]); // 0
free(arr1);
free(arr2);
return 0;
}

15. What is a macro in C?

Answer:
A macro is a preprocessor directive that allows you to define constants or reusable code snippets. Macros are replaced by their values during the preprocessing phase. They are defined using #define.

Example:

#include <stdio.h>
#define PI 3.14159
#define AREA(radius) (PI * radius * radius)
int main() {
int r = 5;
printf("Area of circle: %.2f\n", AREA(r));
return 0;
}

Output: Area of circle: 78.54

16. What is a union in C?

Answer:
A union is a special data type similar to a structure, but in a union, all members share the same memory location. It is used to save memory when only one member will be used at a time.

Example:

#include <stdio.h>
union Data {
int i;
float f;
char str[20];
};
int main() {
union Data d;
d.i = 10;
printf("Integer: %d\n", d.i);
d.f = 5.5;
printf("Float: %.2f\n", d.f);
return 0;
}

Note: Assigning a new value overwrites the previous value.

17. What is the difference between break and continue?

Answer:

  • break: Exits the current loop or switch statement entirely.
  • continue: Skips the remaining statements in the current iteration and moves to the next iteration of the loop.

Example:

#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) continue; // Skips iteration when i == 3
printf("%d ", i);
}
return 0;
}

Output: 1 2 4 5

18. What is the purpose of the return statement in C?

Answer:
The return statement is used to terminate the execution of a function and optionally pass a value back to the calling function.

Example:

#include <stdio.h>
int add(int a, int b) {
return a + b; // Returns the sum of a and b
}
int main() {
int result = add(10, 20);
printf("Result: %d\n", result);
return 0;
}

19. What are bitwise operators in C?

Answer:
Bitwise operators are used to perform operations at the bit level, such as AND, OR, XOR and shifting.

Example:

#include <stdio.h>
int main() {
int a = 5, b = 3; // Binary: 101, 011
printf("AND: %d\n", a & b); // 001 -> 1
printf("OR: %d\n", a | b); // 111 -> 7
return 0;
}

20. What is a static variable in C?

Answer:
A static variable retains its value between function calls. It is initialized only once and has a default value of 0 if not explicitly initialized.

Example:

#include <stdio.h>
void demo() {
static int count = 0; // Static variable
count++;
printf("Count: %d\n", count);
}
int main() {
demo();
demo();
demo();
return 0;
}

Output:

Count: 1  
Count: 2
Count: 3

21. What is the difference between while and do-while loops in C?

Answer:

  • while loop: The condition is checked before executing the loop body. If the condition is false initially, the loop will not execute even once.
  • do-while loop: Executes the loop body at least once, as the condition is checked after the execution.

Example:

#include <stdio.h>
int main() {
int i = 0;
printf("Using while loop:\n");
while (i < 1) { // Condition checked first
printf("This won't execute because i = %d\n", i);
i++;
}

printf("\nUsing do-while loop:\n");
do {
printf("This will execute because condition is checked after\n");
} while (i < 1);
return 0;
}

22. What is a dangling pointer in C?

Answer:
A dangling pointer is a pointer that points to memory that has already been freed or deallocated. Accessing such pointers can cause undefined behavior or program crashes.

Example:

#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int*)malloc(sizeof(int));
*ptr = 42;
free(ptr); // Memory is freed
// ptr is now a dangling pointer
return 0;
}

Solution: Set the pointer to NULL after freeing it:

free(ptr);
ptr = NULL;

23. What are storage classes in C?

Answer:
Storage classes define the scope, lifetime, and visibility of variables. The main storage classes in C are:

  1. Automatic (auto): Default for local variables; scope is within the block.
  2. External (extern): Defines global variables accessible across multiple files.
  3. Static (static): Persists the value between function calls.
  4. Register (register): Suggests storing the variable in a CPU register for faster access.

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

Answer:

  • ++i (pre-increment): Increments the value of i before it is used in an expression.
  • i++ (post-increment): Uses the current value of i in the expression and increments it afterward.

Example:

#include <stdio.h>
int main() {
int i = 5;
printf("Pre-increment: %d\n", ++i); // i becomes 6 before printing
printf("Post-increment: %d\n", i++); // i is printed as 6, then incremented to 7
return 0;
}

25. What is typecasting in C?

Answer:
Typecasting is the conversion of one data type into another. It is either implicit (automatic) or explicit (manual). Explicit typecasting uses the syntax (datatype) variable.

Example:

#include <stdio.h>
int main() {
float num = 5.75;
int integerPart = (int)num; // Explicit typecasting
printf("Integer part: %d\n", integerPart);
return 0;
}

26. What is a segmentation fault in C?

Answer:
A segmentation fault occurs when a program tries to access memory it does not have permission to access or attempts an invalid memory operation (e.g., dereferencing a null or uninitialized pointer).

Example:

#include <stdio.h>
int main() {
int *ptr = NULL; // Null pointer
*ptr = 10; // Dereferencing causes segmentation fault
return 0;
}

27. What are preprocessor directives in C?

Answer:
Preprocessor directives are instructions processed before compilation. They start with # and are used for including files, defining macros or setting conditions.

Common Examples:

  • #include: Includes header files.
  • #define: Defines macros.
  • #ifdef: Conditional compilation.

Example:

#include <stdio.h>
#define PI 3.14
int main() {
printf("Value of PI: %.2f\n", PI);
return 0;
}

28. What is the difference between a pointer and an array in C?

Answer:

  • A pointer is a variable that stores the address of another variable.
  • An array is a collection of elements stored in contiguous memory locations.

Example:

#include <stdio.h>
int main() {
int arr[3] = {1, 2, 3};
int *ptr = arr; // Pointer points to the first element of the array
printf("Array element: %d\n", arr[0]);
printf("Pointer dereference: %d\n", *ptr);
return 0;
}

29. What is a volatile variable in C?

Answer:
A volatile variable is used to inform the compiler that the variable’s value may change at any time, outside the program’s control (e.g., hardware or interrupt signals). This prevents the compiler from optimizing the variable’s access.

Example:

volatile int flag = 0;

30. What are inline functions in C?

Answer:
Inline functions are a way to suggest the compiler to replace the function call with the actual function code to reduce function call overhead. Declared using the inline keyword.

Example:

#include <stdio.h>
inline int square(int x) {
return x * x;
}
int main() {
printf("Square: %d\n", square(5));
return 0;
}

31. What is the purpose of the const keyword in C?

Answer:
The const keyword is used to declare variables whose values cannot be modified after initialization. It helps prevent accidental changes to the variable.

Example:

#include <stdio.h>
int main() {
const int MAX = 100;
// MAX = 200; // Error: Cannot modify a const variable
printf("Max: %d\n", MAX);
return 0;
}

32. What is the difference between fgets() and gets()?

Answer:

  • gets(): Unsafe; does not check buffer overflow.
  • fgets(): Safer; allows specifying the buffer size to avoid overflow.

Example:

#include <stdio.h>
int main() {
char str[20];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin); // Safer alternative
printf("You entered: %s\n", str);
return 0;
}

33. What are function pointers in C?

Answer:
Function pointers are pointers that point to the address of a function. They allow dynamic function calls.

Example:

#include <stdio.h>
void display() {
printf("Function pointer example.\n");
}
int main() {
void (*funcPtr)() = display; // Pointer to display()
funcPtr(); // Calls the function
return 0;
}

34. What is a linked list in C?

Answer:
A linked list is a data structure where elements (nodes) are connected via pointers. Each node contains data and a pointer to the next node.

Example (Node Structure):

struct Node {
int data;
struct Node* next;
};

35. What is a stack in C?

Answer:
A stack is a linear data structure that follows the Last In, First Out (LIFO) principle. Operations include push (insert) and pop (remove).

Example (Push/Pop):

#include <stdio.h>
int stack[10], top = -1;
// Functions for push and pop can be implemented.

36. What is a queue in C?

Answer:
A queue is a linear data structure that follows the First In, First Out (FIFO) principle. Elements are added at the rear (enqueue) and removed from the front (dequeue).

Example:

#include <stdio.h>
#define SIZE 5
int queue[SIZE], front = -1, rear = -1;

// Function to add an element to the queue
void enqueue(int value) {
if (rear == SIZE - 1) {
printf("Queue is full.\n");
} else {
if (front == -1) front = 0;
queue[++rear] = value;
printf("Inserted %d\n", value);
}
}

// Function to remove an element from the queue
void dequeue() {
if (front == -1 || front > rear) {
printf("Queue is empty.\n");
} else {
printf("Removed %d\n", queue[front++]);
}
}

int main() {
enqueue(10);
enqueue(20);
dequeue();
dequeue();
dequeue(); // Queue is empty
return 0;
}

37. What is recursion in C?

Answer:
Recursion is a programming technique where a function calls itself to solve smaller instances of the same problem. It is used for problems like factorial, Fibonacci series, etc.

Example:

#include <stdio.h>
// Recursive function for factorial
int factorial(int n) {
if (n == 0) return 1; // Base case
return n * factorial(n - 1); // Recursive case
}
int main() {
printf("Factorial of 5: %d\n", factorial(5));
return 0;
}

38. What is dynamic memory allocation in C?

Answer:
Dynamic memory allocation allows allocating memory during runtime using functions like malloc(), calloc(), realloc(), and free() from <stdlib.h>.

Example:

#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(5 * sizeof(int)); // Allocates memory for 5 integers
if (ptr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
for (int i = 0; i < 5; i++) {
ptr[i] = i + 1;
printf("%d ", ptr[i]);
}
free(ptr); // Frees allocated memory
return 0;
}

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

Answer:

  • malloc(size): Allocates a block of memory of specified size bytes. The memory is uninitialized.
  • calloc(n, size): Allocates memory for n elements of size bytes each and initializes all bytes to zero.

Example:

#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr1 = (int *)malloc(3 * sizeof(int));
int *arr2 = (int *)calloc(3, sizeof(int));
for (int i = 0; i < 3; i++) {
printf("malloc: %d, calloc: %d\n", arr1[i], arr2[i]);
}
free(arr1);
free(arr2);
return 0;
}

40. What is the difference between struct and union?

Answer:

  • struct: Allocates separate memory for each member, and all members can be accessed simultaneously.
  • union: Allocates shared memory for all members, so only one member can hold a value at a time.

Example:

#include <stdio.h>
struct StructExample {
int a;
float b;
};
union UnionExample {
int a;
float b;
};
int main() {
struct StructExample s = {10, 20.5};
union UnionExample u;
u.a = 10;
printf("Struct: %d, %.1f\n", s.a, s.b);
printf("Union: %d\n", u.a);
return 0;
}

41. What is a macro in C?

Answer:
A macro is a preprocessor directive defined using #define. It substitutes a value or expression before compilation.

Example:

#include <stdio.h>
#define PI 3.14
#define CIRCLE_AREA(r) (PI * (r) * (r))
int main() {
printf("Area of circle: %.2f\n", CIRCLE_AREA(5));
return 0;
}

42. What are bitwise operators in C?

Answer:
Bitwise operators perform operations on binary representations of numbers.

  • & (AND)
  • | (OR)
  • ^ (XOR)
  • ~ (NOT)
  • << (Left shift)
  • >> (Right shift)

Example:

#include <stdio.h>
int main() {
int a = 5, b = 3;
printf("a & b: %d\n", a & b); // 1
printf("a | b: %d\n", a | b); // 7
printf("a ^ b: %d\n", a ^ b); // 6
return 0;
}

43. What is the difference between break and continue?

Answer:

  • break: Terminates the loop entirely.
  • continue: Skips the current iteration and moves to the next.

Example:

#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
if (i == 3) continue; // Skips 3
printf("%d ", i);
}
return 0;
}

44. What is a file in C? How do you handle files?

Answer:
A file is a collection of data stored on disk. File handling in C is done using functions like fopen(), fprintf(), fscanf(), fclose().

Example:

#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "w");
fprintf(file, "Hello, File Handling!");
fclose(file);
return 0;
}

45. What is sizeof operator in C?

Answer:
The sizeof operator returns the size (in bytes) of a variable or datatype.

Example:

#include <stdio.h>
int main() {
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of float: %zu bytes\n", sizeof(float));
return 0;
}

46. What is a function prototype in C?

Answer:
A function prototype declares the function’s name, return type, and parameters before its definition, ensuring proper function usage.

Example:

#include <stdio.h>
void display(); // Prototype
int main() {
display();
return 0;
}
void display() {
printf("Function Prototype Example\n");
}

47. What is the difference between pass by value and pass by reference?

Answer:

  • Pass by Value: A copy of the value is passed; changes don’t affect the original variable.
  • Pass by Reference: The address is passed; changes affect the original variable.

Example (Pass by Reference):

#include <stdio.h>
void changeValue(int *x) {
*x = 10;
}
int main() {
int a = 5;
changeValue(&a);
printf("Value of a: %d\n", a); // 10
return 0;
}

48. What is an enumeration (enum) in C?

Answer:
An enum is a user-defined data type for defining a set of named integral constants.

Example:

#include <stdio.h>
enum Color { RED, GREEN, BLUE };
int main() {
enum Color c = GREEN;
printf("Value of GREEN: %d\n", c); // 1
return 0;
}

49. What is a null pointer in C?

Answer:
A null pointer is a pointer that does not point to any memory location. It is used to indicate that the pointer is uninitialized or empty.

Example:

#include <stdio.h>
int main() {
int *ptr = NULL; // Null pointer
if (ptr == NULL) {
printf("Pointer is NULL.\n");
}
return 0;
}

50. What is the difference between exit() and return in C?

Answer:

  • exit(): Terminates the entire program immediately.
  • return: Exits the current function and passes control back to the caller.

Example:

#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Using exit()\n");
exit(0); // Program ends here
printf("This won't print\n");
return 0;
}

Leave a Comment

BoxofLearn