C Interview Questions
- What is the difference between a definition and a declaration in C?
A definition provides the complete information about a variable or a function, including its type, size, and initial value. A declaration, on the other hand, only provides the information necessary for the compiler to know the name, type, and arguments of a variable or function.
Example:
int x; // definition
extern int y; // declaration
- What is the difference between a local variable and a global variable in C?
A local variable is a variable declared within a function, and it can only be accessed within that function. A global variable, on the other hand, is declared outside of any function and can be accessed from any function in the program.
Example:
int x; // global variable
void func() {
int y; // local variable
}
- What is the difference between a string literal and a character array in C?
A string literal is a sequence of characters surrounded by double quotes, and it is stored in read-only memory. A character array, on the other hand, is a variable that can be modified, and it can store a string by using a null character as the termination of the string.
Example:
char s[] = "hello"; // character array
char *p = "world"; // string literal
- What is the difference between ++i and i++ in C?
The ++i
operator increments the value of i
and then returns the new value, while the i++
operator returns the current value of i
and then increments it.
Example:
int i = 0;
int a = ++i; // a is 1, i is 1
int b = i++; // b is 1, i is 2
- What is the difference between pass-by-value and pass-by-reference in C?
In pass-by-value, a function receives a copy of the argument, and any changes made to the argument within the function do not affect the original argument. In pass-by-reference, a function receives a reference to the argument, and any changes made to the argument within the function affect the original argument.
Example:
void swap(int a, int b) { // pass-by-value
int temp = a;
a = b;
b = temp;
}
void swap(int *a, int *b) { // pass-by-reference
int temp = *a;
*a = *b;
*b = temp;
}
- What is the difference between a pointer and an array in C?
An array is a collection of variables of the same type, while a pointer is a variable that stores the memory address of another variable. An array can be treated as a pointer to its first element.
Example:
int a[5]; // array
int *p; // pointer
p = a; // a can be treated as a pointer to its first element
- What is the use of the
const
keyword in C?
The const
keyword is used to specify that a variable or a pointer is read-only, and its value cannot be changed.
Example:
const int x = 10;
- What is the difference between a
for
loop and awhile
loop in C?
A for
loop is used when you know the number of iterations beforehand, while a while
loop is used when you do not know the number of iterations beforehand. A for
loop has three parts: an initialization, a condition, and an increment/decrement. A while
loop only has a condition.
Example:
for (int i = 0; i < 10; i++) { // for loop
// code to be executed
}
int i = 0;
while (i < 10) { // while loop
// code to be executed
i++;
}
- What is the difference between a
break
statement and acontinue
statement in C?
A break
statement is used to exit a loop early, while a continue
statement is used to skip the current iteration of a loop and continue with the next iteration.
Example:
for (int i = 0; i < 10; i++) {
if (i == 5) {
break; // exit the loop early
}
// code to be executed
}
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue; // skip the current iteration
}
// code to be executed
}
- What is the difference between a
do-while
loop and awhile
loop in C?
A do-while
loop is similar to a while
loop, but with one important difference: the code in a do-while
loop is executed at least once, even if the condition is false, while a while
loop is not executed at all if the condition is false.
Example:
int i = 10;
do {
// code to be executed
i++;
} while (i < 10); // do-while loop
int i = 10;
while (i < 10) { // while loop
// code to be executed
i++;
}
- What is the difference between a static variable and a global variable in C?
A static variable is a variable that is declared within a function and retains its value between function calls, while a global variable is a variable declared outside of any function and can be accessed from any function in the program.
Example:
int x; // global variable
void func() {
static int y; // static variable
}
- What is the difference between the
&&
and||
operators in C?
The &&
operator evaluates to true if both operands are true, while the ||
operator evaluates to true if at least one of the operands is true.
Example:
if (x == 0 && y == 0) { // both operands must be true
// code to be executed
}
if (x == 0 || y == 0) { // at least one operand must be true
// code to be executed
}
- What is a function in C?
A function in C is a block of code that performs a specific task and can be called from other parts of the program. Functions can take parameters and return a value.
Example:
int sum(int a, int b) {
return a + b;
}
int main() {
int result = sum(3, 5);
printf("The sum is %d\n", result);
return 0;
}
- What is a pointer in C?
A pointer in C is a variable that stores the memory address of another variable. Pointers can be used to access and manipulate the memory dynamically.
Example:
int x = 10;
int *ptr = &x; // ptr is a pointer to x
*ptr = 20; // change the value of x through the pointer
- What is a structure in C?
A structure in C is a user-defined data type that groups together variables of different data types under a single name. Structures can be used to store complex data structures such as records.
Example:
struct student {
char name[50];
int age;
float marks;
};
struct student s;
strcpy(s.name, "John Doe");
s.age = 20;
s.marks = 87.5;
- What is a union in C?
A union in C is a user-defined data type that stores multiple variables in the same memory location. Unions are used to save memory by using the same memory location for multiple variables.
Example:
union data {
int x;
float y;
};
union data d;
d.x = 10;
printf("d.x = %d\n", d.x);
d.y = 20.5;
printf("d.y = %f\n", d.y);
- What is a typedef in C?
A typedef in C is a keyword that is used to create a new data type with a specific name. Typedef can be used to simplify the syntax of complex data types and improve code readability.
Example:
typedef struct {
int x;
int y;
} point;
point p;
p.x = 10;
p.y = 20;
- What is a dynamic memory allocation in C?
Dynamic memory allocation in C is a technique to allocate memory at runtime. Dynamic memory allocation is used to allocate memory dynamically, rather than statically, at compile-time.
Example:
int *ptr = (int *) malloc(sizeof(int));
*ptr = 10;
free(ptr);
- What is the purpose of the
#include
directive in C?
The #include
directive in C is used to include header files in the program. Header files contain definitions and prototypes for functions, variables, and other constructs that can be used in the program. The #include
directive makes the definitions and prototypes from the header file available in the program.
Example:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
- What is a macro in C?
A macro in C is a preprocessor directive that defines a constant value or a sequence of statements that can be referred to by a name. Macros are typically used to define constants or simplify repetitive tasks.
Example:
#define PI 3.14
#define MIN(a, b) (a < b ? a : b)
int main() {
double r = 2.0;
double area = PI * r * r;
printf("Area of circle: %f\n", area);
int x = 5, y = 10;
int min = MIN(x, y);
printf("Minimum of %d and %d: %d\n", x, y, min);
return 0;
}
- What is a conditional statement in C?
A conditional statement in C is used to execute a block of code based on a certain condition. The most commonly used conditional statements are if
and switch
.
Example:
int x = 10;
if (x > 0) {
printf("x is positive\n");
}
switch (x) {
case 10:
printf("x is 10\n");
break;
case 20:
printf("x is 20\n");
break;
default:
printf("x is neither 10 nor 20\n");
}
- What is a loop in C?
A loop in C is a control structure that repeats a block of code a certain number of times or until a certain condition is met. The most commonly used loops in C are for
, while
, and do while
.
Example:
int i;
for (i = 1; i <= 10; i++) {
printf("%d\n", i);
}
i = 1;
while (i <= 10) {
printf("%d\n", i);
i++;
}
i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 10);
- What is a function prototype in C?
A function prototype in C is a declaration that provides the compiler with information about the function, including the return type, the number of arguments, and the types of the arguments. Function prototypes are typically declared in header files and included in the program using the #include
directive.
Example:
int sum(int a, int b);
int main() {
int result = sum(3, 5);
printf("The sum is %d\n", result);
return 0;
}
int sum(int a, int b) {
return a + b;
}
- What is a variable scope in C?
A variable scope in C refers to the part of the program where a variable is accessible. Variables can be declared with either global or local scope. Global variables are accessible from any part of the program, while local variables are only accessible within the function where they are declared.
Example:
int x = 10; // global variable
int main() {
int y = 20; // local variable
printf("x = %d, y = %d\n", x, y);
return
- What is typecasting in C?
Typecasting in C is the process of converting a value from one data type to another. This is achieved by explicitly specifying the desired data type within parentheses before the value.
Example:
int x = 10;
float y = (float) x;
printf("x = %d, y = %f\n", x, y);
- What is a pointer in C?
A pointer in C is a variable that holds the memory address of another variable. Pointers are used to manipulate variables indirectly, by accessing the memory location they point to.
Example:
int x = 10;
int *ptr = &x;
printf("Value of x = %d\n", x);
printf("Address of x = %p\n", &x);
printf("Value of ptr = %p\n", ptr);
printf("Value pointed by ptr = %d\n", *ptr);
- What is a dynamic array in C?
A dynamic array in C is an array whose size can be changed during runtime. This is achieved using dynamic memory allocation functions such as malloc
and calloc
.
Example:
int *ptr = (int *) malloc(5 * sizeof(int));
ptr[0] = 1;
ptr[1] = 2;
ptr[2] = 3;
ptr[3] = 4;
ptr[4] = 5;
printf("Dynamic array:\n");
for (int i = 0; i < 5; i++) {
printf("%d\n", ptr[i]);
}
free(ptr);
- What is a linked list in C?
A linked list in C is a data structure that consists of a set of nodes, where each node contains data and a pointer to the next node in the list. Linked lists provide a flexible way to store data, as the size of the list can be changed during runtime.
Example:
struct Node {
int data;
struct Node *next;
};
struct Node *head = NULL;
struct Node *second = NULL;
struct Node *third = NULL;
head = (struct Node *) malloc(sizeof(struct Node));
second = (struct Node *) malloc(sizeof(struct Node));
third = (struct Node *) malloc(sizeof(struct Node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
printf("Linked list:\n");
struct Node *ptr = head;
while (ptr != NULL) {
printf("%d\n", ptr->data);
ptr = ptr->next;
}
free(head);
free(second);
free(third);
- What is a stack in C?
A stack in C is a linear data structure that follows the principle of Last In First Out (LIFO). Elements can be pushed onto the top of the stack, and the last element added is the first one to be removed.
Example:
#define MAX 100
int stack[MAX];
int top = -1;
void push(int value) {
if (top == MAX - 1
- What is a queue in C?
A queue in C is a linear data structure that follows the principle of First In First Out (FIFO). Elements are added to the rear of the queue, and the first element added is the first one to be removed.
Example:
#define MAX 100
int queue[MAX];
int front = -1;
int rear = -1;
void enqueue(int value) {
if (rear == MAX - 1) {
printf("Queue is full\n");
} else {
rear++;
queue[rear] = value;
}
}
int dequeue() {
int value;
if (front == rear) {
printf("Queue is empty\n");
} else {
front++;
value = queue[front];
return value;
}
}
printf("Queue:\n");
enqueue(1);
enqueue(2);
enqueue(3);
printf("%d\n", dequeue());
printf("%d\n", dequeue());
printf("%d\n", dequeue());
- What is a binary search tree in C?
A binary search tree in C is a type of binary tree where the value of each node is greater than the values of all nodes in its left subtree, and less than the values of all nodes in its right subtree. This allows for efficient searching, insertion, and deletion of elements in the tree.
Example:
struct Node {
int data;
struct Node *left;
struct Node *right;
};
struct Node *root = NULL;
void insert(int data) {
struct Node *node = (struct Node *) malloc(sizeof(struct Node));
node->data = data;
node->left = NULL;
node->right = NULL;
if (root == NULL) {
root = node;
} else {
struct Node *ptr = root;
while (ptr != NULL) {
if (data < ptr->data) {
if (ptr->left == NULL) {
ptr->left = node;
break;
} else {
ptr = ptr->left;
}
} else if (data > ptr->data) {
if (ptr->right == NULL) {
ptr->right = node;
break;
} else {
ptr = ptr->right;
}
} else {
printf("Duplicate data\n");
break;
}
}
}
}
printf("Binary Search Tree:\n");
insert(50);
insert(30);
insert(20);
insert(40);
insert(70);
insert(60);
insert(80);
- What is a quick sort algorithm in C?
The quick sort algorithm in C is a divide-and-conquer sorting algorithm that uses a pivot element to partition the array into two sub-arrays. The elements smaller than the pivot are placed in the first sub-array, while the elements larger than the pivot are placed in the second sub-array. The algorithm then recursively sorts both sub-arrays until the entire array is sorted.
Example:
void quick_sort(int arr[], int low, int high) {
int pivot, i, j, temp;
if (low < high) {
pivot = low;
i = low;
j = high;
while (i < j) {
while (arr[i] <= arr[pivot] && i <= high) {
i++;
}
while (arr[j] > arr[pivot] && j >= low) {
j--;
}
if (i < j) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[j];
arr[j] = arr[pivot];
arr[pivot] = temp;
quick_sort(arr, low, j - 1);
quick_sort(arr, j + 1, high);
}
}
printf("Quick Sort:\n");
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
quick_sort(arr, 0, n - 1);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
- What is a selection sort algorithm in C?
The selection sort algorithm in C is a simple sorting algorithm that repeatedly selects the minimum element from the unsorted part of the array and swaps it with the first unsorted element. The algorithm continues until the entire array is sorted.
Example:
void selection_sort(int arr[], int n) {
int i, j, min_idx, temp;
for (i = 0; i < n - 1; i++) {
min_idx = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
printf("Selection Sort:\n");
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
selection_sort(arr, n);
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
- What is a linked list in C?
A linked list in C is a linear data structure where each element is a separate object that is connected to the next element by a pointer. The last element in the list points to a null pointer to indicate the end of the list. Linked lists allow for efficient insertion and deletion operations and can be used to implement data structures such as stacks, queues, and trees.
Example:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void insert_at_end(struct Node **head, int data) {
struct Node *new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = data;
new_node->next = NULL;
if (*head == NULL) {
*head = new_node;
return;
}
struct Node *temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = new_node;
}
void print_list(struct Node *head) {
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}
int main() {
struct Node *head = NULL;
insert_at_end(&head, 1);
insert_at_end(&head, 2);
insert_at_end(&head, 3);
insert_at_end(&head, 4);
print_list(head);
return 0;
}
- What is a stack in C?
A stack in C is a linear data structure that follows the Last In First Out (LIFO) principle. Elements are added to the top of the stack and removed from the top of the stack. This means that the last element added to the stack will be the first one to be removed. Stacks are commonly used to implement algorithms such as depth-first search, undo/redo functionality, and expression evaluation.
Example:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
int stack[MAX_SIZE];
int top = -1;
void push(int data) {
if (top == MAX_SIZE - 1) {
printf("Error: Stack Overflow\n");
return;
}
top++;
stack[top] = data;
}
int pop() {
if (top == -1) {
printf("Error: Stack Underflow\n");
return -1;
}
int data = stack[top];
top--;
return data;
}
int main() {
push(1);
push(2);
push(3);
push(4);
while (top != -1) {
printf("%d ", pop());
}
printf("\n");
return 0;
}
- What is a queue in C?
A queue in C is a linear data structure that follows the First In First Out (FIFO) principle. Elements are added to the back of the queue and removed from the front of the queue. This means that the first element added to the queue will be the first one to be removed. Queues are commonly used to implement algorithms such as breadth-first search, scheduling algorithms, and simulation models.
- What is a binary tree in C?
A binary tree in C is a tree data structure where each node has at most two children, referred to as the left child and the right child. The binary tree is used to implement algorithms such as search trees, expression trees, and decision trees.
Example:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *left;
struct Node *right;
};
struct Node* create_node(int data) {
struct Node *new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
void inorder_traversal(struct Node *root) {
if (root == NULL) {
return;
}
inorder_traversal(root->left);
printf("%d ", root->data);
inorder_traversal(root->right);
}
int main() {
struct Node *root = create_node(1);
root->left = create_node(2);
root->right = create_node(3);
root->left->left = create_node(4);
root->left->right = create_node(5);
inorder_traversal(root);
printf("\n");
return 0;
}
- What is a dynamic array in C?
A dynamic array in C is an array whose size can be changed at runtime. This is achieved by allocating memory dynamically using functions such as malloc
and realloc
. Dynamic arrays allow for more flexibility in terms of memory management and can be used to implement data structures such as lists, stacks, and queues.
Example:
#include <stdio.h>
#include <stdlib.h>
int *create_array(int size) {
int *array = (int*) malloc(sizeof(int) * size);
return array;
}
void resize_array(int **array, int old_size, int new_size) {
int *temp = (int*) realloc(*array, sizeof(int) * new_size);
if (temp == NULL) {
printf("Error: Memory Allocation Failed\n");
return;
}
*array = temp;
}
int main() {
int size = 5;
int *array = create_array(size);
for (int i = 0; i < size; i++) {
array[i] = i + 1;
}
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
size = 10;
resize_array(&array, 5, size);
for (int i = 5; i < size; i++) {
array[i] = i + 1;
}
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
free(array);
Leave a Comment