C Programming Overview and Questions
Overview of C Programming
C is a general-purpose programming language that is widely used for system and application software. Developed in the early 1970s by Dennis Ritchie at Bell Labs, C has influenced many other programming languages and is known for its efficiency and control.
1. Basic Structure of a C Program
A C program typically consists of one or more functions. The most basic structure includes:
#includeint main() { // Code goes here return 0; }
2. Data Types
C provides several built-in data types, including:
- int: for integer values
- float: for single-precision floating-point values
- double: for double-precision floating-point values
- char: for single characters
3. Variables
Variables are used to store data. They must be declared with a specific data type before use. Example:
int age; float salary; char grade;
4. Control Structures
C includes several control structures for decision-making and looping:
- if: used for conditional statements
- for: used for looping a specific number of times
- while: used for looping until a condition is false
- do-while: similar to while but always executes at least once
5. Functions
Functions are blocks of code designed to perform specific tasks. They are defined with a return type, a name, and a set of parameters (optional). Example:
int add(int a, int b) { return a + b; }
6. Arrays
An array is a collection of variables of the same type stored in contiguous memory locations. Example:
int numbers[5] = {1, 2, 3, 4, 5};
7. Pointers
A pointer is a variable that stores the memory address of another variable. They are used for dynamic memory allocation and accessing array elements. Example:
int *ptr; int x = 10; ptr = &x;
Questions and Answers
1. What is the purpose of the `#include` directive in C?
2. How do you declare a variable in C?
int age;
declares an integer variable named age
.
3. What is the difference between `++i` and `i++`?
4. What are the different data types in C?
int
(integer), float
(single-precision floating-point), double
(double-precision floating-point), and char
(character).
5. How do you write a basic function in C?
int add(int a, int b) { return a + b; }
6. What is the use of `return 0;` in the `main` function?
7. How do you initialize an array in C?
int numbers[5] = {1, 2, 3, 4, 5};
8. What is a pointer and how is it used?
int *ptr; int x = 10; ptr = &x;
9. Explain the `if-else` statement.
10. What is the difference between `while` and `do-while` loops?
11. What is the use of `switch` statement?
12. How do you dynamically allocate memory in C?
malloc()
, calloc()
, and realloc()
. For example:
int *ptr = (int *)malloc(sizeof(int) * 10);
13. What is a structure in C?
struct Person { char name[50]; int age; };
14. How do you declare a constant in C?
const
keyword. For example:
const int DAYS_IN_WEEK = 7;
15. What is the purpose of the `sizeof` operator?
16. What are header files and why are they used?
17. How do you handle errors in C?
perror()
and strerror()
can be used to display error messages.
18. What is a `null` pointer?
19. What is the difference between `struct` and `union`?
20. Explain the concept of recursion.
int factorial(int n) { if (n == 0) return 1; return n * factorial(n - 1); }
21. What is the `main` function?
22. How do you perform input and output operations in C?
printf()
for output and scanf()
for input.
23. What is an `enum` in C?
enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY };
24. How do you use command-line arguments in C?
int argc
and char *argv[]
. argc
is the number of arguments, and argv
is an array of strings representing the arguments.
25. What are macros in C?
26. What is the purpose of `#define` directive?
#define PI 3.14159
27. How do you perform bitwise operations in C?
&
(AND), |
(OR), ^
(XOR), ~
(NOT), and bit shifts <<
(left shift), >>
(right shift).
28. What is the purpose of the `void` pointer?
29. How do you handle multi-dimensional arrays in C?
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
30. What are the different storage classes in C?
auto
, register
, static
, and extern
. They define the scope, lifetime, and visibility of variables.
31. How do you create a user-defined header file?
32. What is the purpose of the `break` and `continue` statements?
33. What is the use of `extern` keyword?
34. How do you handle strings in C?
strcpy()
and strlen()
, are commonly used for string operations.
35. What is a dangling pointer?
36. Explain the `sizeof` operator with an example.
printf("Size of int: %lu bytes\n", sizeof(int));
37. How do you declare and initialize a pointer?
int x = 10; int *ptr = &x;
38. What is a function prototype?
39. How do you use the `switch` statement?
switch (day) { case 1: printf("Sunday"); break; case 2: printf("Monday"); break; // other cases default: printf("Invalid day"); }
40. What is the difference between `malloc` and `calloc`?
int *arr1 = (int *)malloc(10 * sizeof(int)); int *arr2 = (int *)calloc(10, sizeof(int));
Questions and Answers
41. What is a static variable?
42. How do you create a multi-dimensional array?
int matrix[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} };
43. What is the difference between `fgets()` and `scanf()`?
44. Explain the use of the `continue` statement in loops.
45. How do you handle memory deallocation in C?
free(ptr);
46. What is a function pointer?
void (*funcPtr)(); funcPtr = &someFunction; (*funcPtr)();
47. What is the difference between `==` and `=`?
48. How do you prevent a function from being inlined?
49. What is the purpose of the `extern` keyword?
50. Explain the `sizeof` operator with examples.
printf("Size of int: %lu bytes\n", sizeof(int)); printf("Size of double: %lu bytes\n", sizeof(double));
51. What is a dynamic array?
52. How do you use the `assert` macro?
#includeassert(x > 0);
53. What is the difference between `struct` and `class` in C++?
54. How do you handle file I/O in C?
FILE *file = fopen("file.txt", "r"); fclose(file);
55. What are preprocessor directives?
56. What is the use of `#ifdef` and `#endif`?
#ifdef DEBUG printf("Debug mode\n"); #endif
57. How do you handle command-line arguments?
58. What is the `typedef` keyword used for?
typedef unsigned long ulong; ulong x;
59. What is the purpose of the `union` data type?
60. How do you use `goto` statement in C?
goto label; label: printf("This is a label");
61. What is a `volatile` keyword in C?
62. What is the difference between `fopen()` and `freopen()`?
FILE *file = freopen("file.txt", "w", stdout);
63. How do you use the `return` statement?
int add(int a, int b) { return a + b; }
64. What is a `null` pointer and how is it used?
65. What is the difference between `struct` and `union`?
66. What is the purpose of `#include` directive?
#include
67. How do you declare a function in C?
int add(int a, int b);
68. What is the purpose of the `static` keyword?
69. What are inline functions and how are they used?
inline int square(int x) { return x * x; }
70. What is a preprocessor in C?
71. How do you handle multi-dimensional arrays?
int matrix[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} };
72. What is a `volatile` keyword used for?
73. What is the difference between `strcpy()` and `strcat()`?
strcpy(dest, src); strcat(dest, src);
74. How do you manage memory with `malloc()` and `calloc()`?
int *arr1 = (int *)malloc(10 * sizeof(int)); int *arr2 = (int *)calloc(10, sizeof(int));
75. What is a `typedef` and how is it used?
typedef unsigned int uint; uint x;
76. How do you perform error handling in C?
if (file == NULL) { perror("Error opening file"); }
77. What are the different types of operators in C?
int a = 10 + 5; // Arithmetic if (a > 10) {} // Relational if (a && b) {} // Logical a |= b; // Bitwise a = 5; // Assignment result = (a > b) ? a : b; // Conditional
78. How do you use `switch` statement in C?
switch (day) { case 1: printf("Sunday"); break; case 2: printf("Monday"); break; default: printf("Invalid day"); }
79. What is the `const` keyword used for?
const int daysInWeek = 7;
80. How do you handle command-line arguments in C?
81. How do you declare an array of pointers?
int *arr[5];
82. What is the purpose of `#ifndef` directive?
#ifndef HEADER_FILE #define HEADER_FILE // header file contents #endif
83. How do you handle string manipulation in C?
char str1[20] = "Hello"; char str2[20] = "World"; strcat(str1, str2);
84. What is the use of the `sizeof` operator?
printf("Size of int: %lu bytes\n", sizeof(int));
85. How do you initialize a pointer variable?
int x = 10; int *ptr = &x;
86. What is the difference between `++i` and `i++`?
int i = 5; int a = ++i; // a = 6, i = 6 int b = i++; // b = 6, i = 7
87. How do you pass an array to a function?
void printArray(int arr[], int size) { for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } }
88. What is the purpose of `#define` directive?
#define PI 3.14
89. How do you use `fgets()` for reading strings?
char str[100]; fgets(str, sizeof(str), stdin);
90. What is a macro and how is it used in C?
#define MAX 100 int arr[MAX];
91. What are bitwise operators and how are they used?
int a = 5; // 0101 int b = 3; // 0011 int c = a & b; // 0001
92. How do you use the `return` statement in a function?
int multiply(int a, int b) { return a * b; }
93. What is a `goto` statement and how is it used?
goto label; label: printf("This is a label");
94. What are the different storage classes in C?
95. How do you use the `register` storage class?
register int count;
96. What is a `typedef` and how does it simplify code?
typedef unsigned long ulong; ulong x;
97. How do you perform bit manipulation in C?
int a = 5; // 0101 int b = a << 1; // 1010
98. What is the difference between `strlen()` and `sizeof()`?
printf("Length of str: %lu\n", strlen(str)); printf("Size of str: %lu\n", sizeof(str));
99. How do you define and use constants in C?
#define PI 3.14 const int DAYS_IN_WEEK = 7;
100. What is the purpose of `assert()` function?
#includeassert(x > 0);
101. How do you declare and use a `static` variable?
void counter() { static int count = 0; count++; printf("%d\n", count); }
102. What is a `union` and how is it different from a `struct`?
union Data { int i; float f; }; struct Person { char name[50]; int age; };
103. How do you use the `enum` keyword in C?
enum Week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }; enum Week today = Monday;
104. What is the purpose of `#pragma` directive?
#pragma once
105. How do you define and use a function pointer?
void (*funcPtr)(int) = &function; funcPtr(10);
106. What is the purpose of `extern` keyword?
extern int globalVar;
107. How do you handle file I/O operations in C?
FILE *file = fopen("file.txt", "r"); fclose(file);
108. What is a `typedef` and how is it different from `#define`?
typedef unsigned long ulong; #define MAX_SIZE 100
109. How do you handle dynamic memory allocation in C?
int *arr = (int *)malloc(10 * sizeof(int)); free(arr);
110. What are the differences between `struct` and `class` in C++?
struct MyStruct { int x; }; class MyClass { int x; };
111. What is the purpose of `#include` directive in C?
112. How do you declare and use an array of structures?
struct Person { char name[50]; int age; }; struct Person people[10];
113. What is the purpose of the `volatile` keyword?
114. How do you use `scanf()` for input in C?
int num; scanf("%d", &num);
115. What is the difference between `calloc()` and `malloc()`?
int *arr1 = (int *)calloc(10, sizeof(int)); int *arr2 = (int *)malloc(10 * sizeof(int));
116. How do you use `printf()` for formatted output?
printf("Integer: %d, Float: %.2f\n", 10, 3.14);
117. What is a `function pointer` and how is it used?
void (*funcPtr)(int) = &function; funcPtr(10);
118. How do you use `fseek()` in file handling?
fseek(file, 0, SEEK_SET); // Move to the beginning of the file
119. What is the purpose of `assert()` in C?
#includeassert(x > 0);
120. How do you use `sizeof()` operator?
printf("Size of int: %lu bytes\n", sizeof(int));
121. What is a `macro` in C and how is it used?
#define MAX 100
122. How do you use `fclose()` in file handling?
FILE *file = fopen("file.txt", "r"); fclose(file);
123. What is the purpose of `extern` keyword in C?
124. How do you use `strcpy()` and `strcat()`?
strcpy(dest, src); strcat(dest, src);
125. What are `enum` types in C and how are they used?
enum Color { RED, GREEN, BLUE }; enum Color favoriteColor = GREEN;
126. What is the difference between `int` and `unsigned int`?
int a = -5; unsigned int b = 5;
127. How do you use `malloc()` and `free()`?
int *ptr = (int *)malloc(sizeof(int)); free(ptr);
128. What is the difference between `++i` and `i++` in loops?
129. How do you use `fread()` and `fwrite()` for file handling?
fread(buffer, sizeof(char), size, file); fwrite(buffer, sizeof(char), size, file);
130. What is a `static` function in C?
static void helperFunction() { // function code }
131. How do you use `strncpy()` and `strncat()`?
strncpy(dest, src, 5); strncat(dest, src, 5);
132. What is the use of `sizeof()` operator with arrays?
int arr[10]; size_t size = sizeof(arr) / sizeof(arr[0]);
133. How do you handle string formatting in C?
char buffer[100]; sprintf(buffer, "Formatted string: %d", 10);
134. What is a `null` pointer and how is it used?
int *ptr = NULL;
135. How do you use `strtol()` and `strtoul()`?
long int value = strtol("12345", NULL, 10); unsigned long int uvalue = strtoul("12345", NULL, 10);
136. What is a `volatile` variable and why is it used?
137. How do you handle command-line arguments in C?
int main(int argc, char *argv[]) { for (int i = 0; i < argc; i++) { printf("Argument %d: %s\n", i, argv[i]); } return 0; }
138. What is the difference between `char *` and `const char *`?
char *str = "Hello"; const char *cstr = "World";
139. What is the purpose of the `register` keyword?
140. How do you declare a multi-dimensional array in C?
int matrix[3][4];
141. What is the difference between `fopen()` and `freopen()`?
FILE *file = fopen("file.txt", "r"); file = freopen("newfile.txt", "w", file);
142. How do you use `rewind()` in file handling?
rewind(file);
143. What is a `pointer` in C?
int x = 10; int *ptr = &x;
144. What is a `default` argument in C++?
void greet(int age = 18) { printf("Age: %d\n", age); }
145. How do you use `sizeof()` with pointer variables?
int *ptr; printf("Size of pointer: %lu\n", sizeof(ptr));
146. What is a `function` in C?
int add(int a, int b) { return a + b; }
147. How do you use `fwrite()` for binary file writing?
FILE *file = fopen("file.bin", "wb"); fwrite(buffer, sizeof(char), size, file); fclose(file);
148. How do you use `fread()` for binary file reading?
FILE *file = fopen("file.bin", "rb"); fread(buffer, sizeof(char), size, file); fclose(file);
149. What is a `linked list` in C?
struct Node { int data; struct Node *next; };
150. How do you use `fseek()` for seeking in a file?
fseek(file, 10, SEEK_SET); // Move to the 10th byte from the start
151. What is the difference between `struct` and `union` in C?
struct Data { int i; float f; }; union Data { int i; float f; };
152. How do you implement a stack using an array in C?
#define MAX 100 int stack[MAX]; int top = -1; void push(int value) { if (top < MAX - 1) { stack[++top] = value; } } int pop() { if (top >= 0) { return stack[top--]; } return -1; // Stack underflow }
153. What is the purpose of `static` keyword in C?
154. How do you create a doubly linked list in C?
struct Node { int data; struct Node *next; struct Node *prev; };
155. What is the `exit()` function used for?
#includeexit(0);
156. How do you handle errors in file operations?
if (ferror(file)) { printf("Error occurred while reading the file.\n"); }
157. What is a `buffer overflow` and how can it be prevented?
158. How do you implement a queue using an array in C?
#define MAX 100 int queue[MAX]; int front = -1, rear = -1; void enqueue(int value) { if (rear < MAX - 1) { if (front == -1) front = 0; queue[++rear] = value; } } int dequeue() { if (front <= rear && front != -1) { return queue[front++]; } return -1; // Queue underflow }
159. What is a `macro` and how is it different from a function?
160. How do you use `memcpy()` in C?
#includememcpy(dest, src, sizeof(src));
161. What is a `preprocessor directive` and provide examples?
#include#define MAX 100
162. What are `function prototypes` and why are they used?
void myFunction(int x);
163. What is `memory alignment` and why is it important?
164. How do you use `strtok()` to tokenize a string?
#includechar str[] = "Hello,world"; char *token = strtok(str, ","); while (token != NULL) { printf("%s\n", token); token = strtok(NULL, ","); }
165. What is a `bit field` in C?
struct Flags { unsigned int flag1 : 1; unsigned int flag2 : 3; };
166. How do you use `strchr()` to find a character in a string?
char *result = strchr("Hello", 'e'); if (result) { printf("Character found: %s\n", result); }
167. What is the purpose of `volatile` keyword in C?
168. How do you create and use a binary tree in C?
struct Node { int data; struct Node *left; struct Node *right; };
169. How do you perform a binary search in C?
int binarySearch(int arr[], int size, int target) { int low = 0, high = size - 1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) low = mid + 1; else high = mid - 1; } return -1; // Element not found }
170. What are `inline functions` and how do you use them?
inline int square(int x) { return x * x; }
171. What is the difference between `malloc()` and `calloc()`?
int *arr = (int *)malloc(10 * sizeof(int)); int *arr_zeroed = (int *)calloc(10, sizeof(int));
172. How do you use `strspn()` to find the length of a substring?
size_t len = strspn("Hello123", "Hello123");
173. What is `dynamic memory allocation` in C?
174. How do you use `qsort()` for sorting arrays?
#includeint compare(const void *a, const void *b) { return (*(int *)a - *(int *)b); } int arr[] = {4, 2, 3, 1}; qsort(arr, 4, sizeof(int), compare);
175. What is `function overloading` and does C support it?
176. How do you use `fprintf()` for formatted output to a file?
FILE *file = fopen("file.txt", "w"); fprintf(file, "Value: %d\n", 100); fclose(file);
177. What is a `null terminator` in C strings?
178. How do you implement a `priority queue` in C?
#define MAX 100 int heap[MAX]; int size = 0; void insert(int value) { // Insert value into heap and maintain heap property } int extractMax() { // Remove and return max value from heap }
179. How do you use `fclose()` in file handling?
FILE *file = fopen("file.txt", "r"); fclose(file);
180. What is the purpose of `const` keyword in C?
181. How do you use `typedef` to create an alias for a type?
typedef unsigned long ulong; ulong num = 1000;
182. What are `function pointers` and how are they used?
void (*funcPtr)() = &myFunction; funcPtr();
183. How do you handle `variable argument lists` using `stdarg.h`?
#includevoid printNumbers(int count, ...) { va_list args; va_start(args, count); for (int i = 0; i < count; i++) { printf("%d ", va_arg(args, int)); } va_end(args); }
184. What is a `default argument` in C++ and does C support it?
185. How do you use `strcpy()` to copy strings?
char src[] = "Hello"; char dest[50]; strcpy(dest, src);
186. What is a `hash table` and how is it implemented in C?
#define TABLE_SIZE 100 int table[TABLE_SIZE]; void insert(int key, int value) { int index = key % TABLE_SIZE; table[index] = value; } int search(int key) { int index = key % TABLE_SIZE; return table[index]; }
187. How do you implement `bubble sort` in C?
void bubbleSort(int arr[], int n) { for (int i = 0; i < n-1; i++) { for (int j = 0; j < n-i-1; j++) { if (arr[j] > arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } }
188. How do you use `sizeof()` to determine the size of a data type?
int size = sizeof(int);
189. What is the purpose of `volatile` keyword in C?
190. How do you implement a `hash function` for a hash table?
int hashFunction(int key) { return key % TABLE_SIZE; }
191. What is a `macro` in C and provide examples?
#define PI 3.14 #define SQUARE(x) ((x) * (x))
192. How do you use `strcat()` to concatenate strings?
char dest[50] = "Hello, "; char src[] = "World!"; strcat(dest, src);
193. What is a `stack overflow` and how can it be prevented?
194. How do you implement a `circular queue` in C?
#define SIZE 100 int queue[SIZE]; int front = 0, rear = 0; void enqueue(int value) { queue[rear] = value; rear = (rear + 1) % SIZE; } int dequeue() { int value = queue[front]; front = (front + 1) % SIZE; return value; }
195. What are `function-like macros` and how do they differ from functions?
196. How do you use `fprintf()` for formatted output to a file?
FILE *file = fopen("file.txt", "w"); fprintf(file, "Number: %d\n", 123); fclose(file);
197. What is `stack memory` and how is it managed in C?
198. What is `heap memory` and how is it managed in C?
199. How do you use `strncat()` to concatenate a specific number of characters from a string?
char dest[20] = "Hello, "; char src[] = "World!"; strncat(dest, src, 3); // Appends "Wor" to dest
200. How do you implement a `binary search tree` (BST) in C?
struct Node { int data; struct Node *left; struct Node *right; }; struct Node* insert(struct Node* node, int key) { if (node == NULL) { node = (struct Node*)malloc(sizeof(struct Node)); node->data = key; node->left = node->right = NULL; } else if (key < node->data) { node->left = insert(node->left, key); } else { node->right = insert(node->right, key); } return node; }
201. What is the `sizeof` operator and how is it used in C?
int size = sizeof(int);
202. How do you use `strcmp()` to compare two strings?
int result = strcmp("abc", "abc"); // result will be 0
203. What are `enumerations` in C and how are they used?
enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }; enum Day today = Monday;
204. What is the `return` keyword used for in C functions?
int add(int a, int b) { return a + b; }
205. How do you use `fread()` and `fwrite()` for file operations?
FILE *file = fopen("file.bin", "wb"); int data = 123; fwrite(&data, sizeof(int), 1, file); fclose(file); FILE *file = fopen("file.bin", "rb"); int data; fread(&data, sizeof(int), 1, file); fclose(file);
206. How do you use `fscanf()` to read formatted data from a file?
FILE *file = fopen("file.txt", "r"); int number; fscanf(file, "%d", &number); fclose(file);
207. How do you implement a `circular buffer` in C?
#define SIZE 100 int buffer[SIZE]; int start = 0, end = 0; void add(int value) { buffer[end] = value; end = (end + 1) % SIZE; } int remove() { int value = buffer[start]; start = (start + 1) % SIZE; return value; }
208. How do you use `sprintf()` for formatted output to a string?
char buffer[50]; sprintf(buffer, "Value: %d", 100);
209. What is the purpose of `static` keyword in C?
210. How do you use `fseek()` to reposition the file pointer?
FILE *file = fopen("file.txt", "r"); fseek(file, 10, SEEK_SET); // Move file pointer to 10 bytes from the start fclose(file);
211. How do you use `calloc()` for dynamic memory allocation?
int *arr = (int *)calloc(10, sizeof(int));
212. What is `volatile` keyword and its purpose in C?
213. How do you use `strlen()` to find the length of a string?
size_t len = strlen("Hello");
214. What is a `linked list` and how is it implemented in C?
struct Node { int data; struct Node *next; }; void append(struct Node **head, int value) { struct Node *new_node = (struct Node *)malloc(sizeof(struct Node)); new_node->data = value; new_node->next = *head; *head = new_node; }
215. How do you use `fputs()` to write strings to a file?
FILE *file = fopen("file.txt", "w"); fputs("Hello, World!", file); fclose(file);
216. How do you use `fflush()` to flush the output buffer?
FILE *file = fopen("file.txt", "w"); fprintf(file, "Hello, World!"); fflush(file); fclose(file);
217. What is the purpose of `goto` statement in C?
218. How do you use `vprintf()` for variable argument lists?
#includevoid logMessage(const char *format, ...) { va_list args; va_start(args, format); vprintf(format, args); va_end(args); }
219. What is the purpose of `enum` in C?
enum Color { RED, GREEN, BLUE }; enum Color myColor = GREEN;
220. How do you use `fgets()` to read a line from a file?
FILE *file = fopen("file.txt", "r"); char buffer[100]; fgets(buffer, sizeof(buffer), file); fclose(file);
221. How do you use `memcpy()` to copy memory?
#includechar src[] = "Hello"; char dest[6]; memcpy(dest, src, sizeof(src));
222. What is a `binary tree` and how is it implemented in C?
struct Node { int data; struct Node *left; struct Node *right; }; struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->left = newNode->right = NULL; return newNode; }
223. How do you implement a `queue` using stacks in C?
#define MAX 100 struct Stack { int arr[MAX]; int top; }; void push(struct Stack *s, int value) { s->arr[++(s->top)] = value; } int pop(struct Stack *s) { return s->arr[(s->top)--]; } void enqueue(struct Stack *s1, struct Stack *s2, int value) { push(s1, value); } int dequeue(struct Stack *s1, struct Stack *s2) { if (s2->top == -1) { while (s1->top != -1) { push(s2, pop(s1)); } } return pop(s2); }
224. What is a `hash table` and how is it implemented in C?
#define TABLE_SIZE 10 struct HashTable { int table[TABLE_SIZE]; }; int hashFunction(int key) { return key % TABLE_SIZE; } void insert(struct HashTable *ht, int key, int value) { int index = hashFunction(key); ht->table[index] = value; } int get(struct HashTable *ht, int key) { int index = hashFunction(key); return ht->table[index]; }
225. How do you handle errors in file operations in C?
FILE *file = fopen("file.txt", "r"); if (file == NULL) { perror("Error opening file"); } fclose(file);
226. What is the purpose of `const` keyword in C?
const int max = 100;
227. How do you use `strchr()` to find a character in a string?
char str[] = "Hello, World!"; char *ptr = strchr(str, 'W'); // ptr points to "World!"
228. What are `function pointers` and how are they used in C?
void printMessage() { printf("Hello World!\n"); } void (*funcPtr)() = printMessage; funcPtr(); // Calls printMessage
229. How do you implement a `priority queue` in C?
#include#include #define MAX 100 typedef struct { int data[MAX]; int size; } PriorityQueue; void insert(PriorityQueue *pq, int value) { int i = pq->size++; pq->data[i] = value; while (i > 0 && pq->data[i] > pq->data[(i - 1) / 2]) { int temp = pq->data[i]; pq->data[i] = pq->data[(i - 1) / 2]; pq->data[(i - 1) / 2] = temp; i = (i - 1) / 2; } } int extractMax(PriorityQueue *pq) { int max = pq->data[0]; pq->data[0] = pq->data[--pq->size]; int i = 0; while (2 * i + 1 < pq->size) { int j = 2 * i + 1; if (j + 1 < pq->size && pq->data[j] < pq->data[j + 1]) { j++; } if (pq->data[i] >= pq->data[j]) { break; } int temp = pq->data[i]; pq->data[i] = pq->data[j]; pq->data[j] = temp; i = j; } return max; }
230. How do you use `assert()` for debugging in C?
#includevoid checkValue(int value) { assert(value > 0); }
231. How do you use `va_list`, `va_start`, and `va_end` for variable arguments?
#include#include void printNumbers(int count, ...) { va_list args; va_start(args, count); for (int i = 0; i < count; i++) { printf("%d\n", va_arg(args, int)); } va_end(args); }
232. How do you use `realloc()` to resize a dynamic array?
int *arr = (int *)malloc(5 * sizeof(int)); arr = (int *)realloc(arr, 10 * sizeof(int));
233. What is a `macro` in C and how does it differ from a `function`?
234. How do you use `strtok()` to tokenize a string?
char str[] = "Hello,World"; char *token = strtok(str, ","); while (token != NULL) { printf("%s\n", token); token = strtok(NULL, ","); }
235. What is the purpose of `typedef` in C?
typedef unsigned long ulong; ulong num = 123456789;
236. How do you implement a `stack` using arrays in C?
#define MAX 100 struct Stack { int arr[MAX]; int top; }; void push(struct Stack *s, int value) { if (s->top < MAX - 1) { s->arr[++(s->top)] = value; } } int pop(struct Stack *s) { if (s->top >= 0) { return s->arr[(s->top)--]; } return -1; // Stack underflow }
237. How do you use `fscanf()` to read formatted data from a file?
FILE *file = fopen("file.txt", "r"); int value; fscanf(file, "%d", &value); fclose(file);
238. How do you use `sprintf()` to format strings?
char buffer[50]; sprintf(buffer, "The number is %d", 123);
239. What is the difference between `memcpy()` and `memmove()`?
240. How do you implement a `circular queue` in C?
#define MAX 100 struct CircularQueue { int arr[MAX]; int front, rear; }; void enqueue(struct CircularQueue *q, int value) { q->rear = (q->rear + 1) % MAX; q->arr[q->rear] = value; } int dequeue(struct CircularQueue *q) { int value = q->arr[q->front]; q->front = (q->front + 1) % MAX; return value; }
241. What is a `void pointer` and how is it used in C?
void *ptr; int value = 10; ptr = &value; printf("%d\n", *(int *)ptr);
242. How do you use `size_t` for size-related operations?
size_t length = strlen("Hello");
243. What is `dynamic memory allocation` in C and how is it done?
int *arr = (int *)malloc(10 * sizeof(int)); free(arr);
244. How do you use `fread()` to read data from a file?
FILE *file = fopen("file.bin", "rb"); int buffer[10]; fread(buffer, sizeof(int), 10, file); fclose(file);
245. What is the `struct` keyword used for in C?
struct Person { char name[50]; int age; }; struct Person p1 = {"John Doe", 30};
246. How do you use `fclose()` to close a file?
FILE *file = fopen("file.txt", "w"); fprintf(file, "Hello, World!"); fclose(file);
247. What is a `file pointer` in C and how is it used?
FILE *file = fopen("file.txt", "r");
248. How do you use `feof()` to check the end of a file?
FILE *file = fopen("file.txt", "r"); while (!feof(file)) { // Read file contents } fclose(file);
249. What is `printf()` used for in C?
printf("The value is %d\n", 10);
250. How do you use `scanf()` to read formatted input from the user?
int value; scanf("%d", &value);
251. How do you use `fprintf()` to write formatted output to a file?
FILE *file = fopen("file.txt", "w"); fprintf(file, "The value is %d\n", 123); fclose(file);
252. What is the `sizeof` operator and how is it used in C?
int size = sizeof(int);
253. How do you use `memset()` to set memory with a constant value?
int arr[10]; memset(arr, 0, sizeof(arr)); // Set all elements to 0
254. What are `function declarations` and how do they differ from `definitions`?
// Declaration void myFunction(int a, int b); // Definition void myFunction(int a, int b) { // function body }
255. How do you use `atoi()` to convert a string to an integer?
int value = atoi("12345");
256. What is a `dynamic array` and how is it managed in C?
int *arr = (int *)malloc(10 * sizeof(int)); arr = (int *)realloc(arr, 20 * sizeof(int)); free(arr);
257. How do you use `fseek()` to move the file pointer?
FILE *file = fopen("file.txt", "r"); fseek(file, 5, SEEK_SET); // Move to 5 bytes from the start fclose(file);
258. What is the difference between `char` and `unsigned char`?
259. How do you implement a `binary search` algorithm in C?
int binarySearch(int arr[], int size, int target) { int left = 0, right = size - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) return mid; if (arr[mid] < target) left = mid + 1; else right = mid - 1; } return -1; }
260. What is the purpose of `struct` padding and how can it be controlled?
261. How do you use `printf()` with different format specifiers?
printf("Integer: %d, Float: %.2f, String: %s\n", 10, 3.14, "Hello");
262. What is the `main()` function and why is it important?
263. How do you handle `memory leaks` in C?
264. How do you use `assert()` to check for conditions in C?
#includeassert(x > 0);
265. What are `bit fields` and how are they used in C?
struct { unsigned int b1 : 3; unsigned int b2 : 5; } bits;
266. How do you implement a `graph` using an adjacency list in C?
#include#include struct Node { int vertex; struct Node* next; }; struct Graph { int numVertices; struct Node** adjLists; }; struct Graph* createGraph(int vertices) { struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph)); graph->numVertices = vertices; graph->adjLists = (struct Node**)malloc(vertices * sizeof(struct Node*)); for (int i = 0; i < vertices; i++) { graph->adjLists[i] = NULL; } return graph; } void addEdge(struct Graph* graph, int src, int dest) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->vertex = dest; newNode->next = graph->adjLists[src]; graph->adjLists[src] = newNode; }
267. How do you use `fopen()` with different modes?
FILE *file = fopen("file.txt", "w"); // Open for writing
268. What is the `return` statement used for in C functions?
int add(int a, int b) { return a + b; }
269. How do you use `enum` in C?
enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }; enum Day today = Wednesday;
270. What are `pointers to functions` and how are they used?
void hello() { printf("Hello World!\n"); } void (*funcPtr)() = hello; funcPtr();
271. How do you use `calloc()` for dynamic memory allocation?
int *arr = (int *)calloc(10, sizeof(int)); free(arr);
272. What is `return by reference` and how is it implemented in C?
void setValue(int *p) { *p = 10; } int main() { int x; setValue(&x); printf("%d\n", x); // Output will be 10 }
273. How do you use `fputs()` to write a string to a file?
FILE *file = fopen("file.txt", "w"); fputs("Hello, World!", file); fclose(file);
274. What is the `enum` keyword used for in C?
275. How do you use `strtol()` to convert a string to a long integer?
long value = strtol("12345", NULL, 10);
276. What is `type casting` and how is it performed in C?
int a = 10; float b = (float)a;
277. How do you use `memchr()` to locate a character in memory?
char str[] = "Hello"; char *ptr = (char *)memchr(str, 'e', sizeof(str));
278. What are `macros` and how are they defined in C?
#define PI 3.14
279. How do you use `strncpy()` to copy a substring?
char src[] = "Hello World"; char dest[6]; strncpy(dest, src, 5); dest[5] = '\0'; // Null-terminate
280. How do you use `strspn()` to get the length of the initial segment of a string?
size_t len = strspn("abc123", "abc");
281. What are `linked lists` and how are they implemented in C?
struct Node { int data; struct Node* next; }; void insert(struct Node** head, int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = *head; *head = newNode; }
282. How do you use `strrchr()` to find the last occurrence of a character?
char *ptr = strrchr("Hello World", 'o');
283. What is a `union` and how is it used in C?
union Data { int i; float f; char str[20]; }; union Data data; data.i = 10;
284. How do you use `memmove()` to move memory blocks?
char src[] = "Hello World"; memmove(src + 6, src, 5);
285. What is `pointer arithmetic` and how is it performed?
int arr[5]; int *ptr = arr; ptr += 2; // Move the pointer to the third element
286. How do you use `strcat()` to concatenate strings?
char dest[20] = "Hello"; char src[] = " World"; strcat(dest, src);
287. What is `const` and how is it used in C?
const int MAX = 100;
288. How do you use `sscanf()` to parse a string?
int a, b; sscanf("10 20", "%d %d", &a, &b);
289. How do you implement a `queue` using arrays in C?
#define MAX 100 struct Queue { int arr[MAX]; int front, rear; }; void enqueue(struct Queue *q, int value) { q->arr[q->rear++] = value; } int dequeue(struct Queue *q) { return q->arr[q->front++]; }
290. What is `volatile` and when should it be used?
volatile int flag;
291. How do you use `fscanf()` to read formatted input from a file?
FILE *file = fopen("file.txt", "r"); int num; fscanf(file, "%d", &num); fclose(file);
292. What is `typedef` and how is it used in C?
typedef unsigned long ulong; ulong value = 1000;
293. How do you implement a `stack` using arrays in C?
#define MAX 100 struct Stack { int arr[MAX]; int top; }; void push(struct Stack *s, int value) { s->arr[++s->top] = value; } int pop(struct Stack *s) { return s->arr[s->top--]; }
294. What is `goto` and how should it be used in C?
goto label; label: printf("Jumped to label");
295. How do you use `rand()` to generate random numbers in C?
int num = rand() % 100; // Random number between 0 and 99
296. How do you use `scanf()` to read formatted input?
int num; scanf("%d", &num);
297. What are `function pointers` and how are they used in C?
void (*funcPtr)(int) = someFunction; funcPtr(10);
298. How do you use `fseek()` to set file position indicator?
fseek(file, 0, SEEK_SET); // Move to the start of the file
299. What is `malloc()` and how is it used for memory allocation?
int *arr = (int *)malloc(10 * sizeof(int)); free(arr);
300. How do you use `free()` to deallocate memory?
int *arr = (int *)malloc(10 * sizeof(int)); free(arr);
0 Comments