Interview questions on C are an essential part of the selection process for many programming positions, especially in the field of software development. These questions help assess a candidate’s understanding of the C programming language, their problem-solving skills, and their ability to apply C concepts in real-world scenarios. In this article, we will explore some common interview questions on C, along with their answers and explanations, to help candidates prepare for their interviews.
1. What is the difference between a pointer and an array in C?
An array is a collection of elements of the same data type, whereas a pointer is a variable that stores the memory address of another variable. The main differences are:
– An array is a contiguous block of memory, while a pointer can point to any memory location.
– The size of an array is fixed at the time of declaration, while a pointer can be reassigned to point to different memory locations.
– An array name acts as a pointer to its first element, but an array cannot be used as a pointer.
2. How do you pass an array to a function in C?
An array can be passed to a function in C by either passing the address of its first element or using a pointer to the array. Here’s an example:
“`c
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("");
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
printArray(arr, size);
return 0;
}
```
In this example, the array `arr` is passed to the `printArray` function using its name, which acts as a pointer to the first element of the array.
3. What is the difference between `const` and `volatile` in C?
– `const`: A `const` variable is a read-only variable. Its value cannot be changed after initialization. It is used to ensure that the variable’s value remains constant throughout the program.
– `volatile`: A `volatile` variable is a variable whose value can be changed by means other than the program, such as hardware or another thread. It is used to inform the compiler that the variable’s value can change unexpectedly and should not be optimized.
4. How do you allocate memory dynamically in C?
Memory can be allocated dynamically in C using the `malloc`, `calloc`, and `realloc` functions. Here’s an example:
“`c
include
include
int main() {
int ptr = (int)malloc(5 sizeof(int));
if (ptr == NULL) {
printf(“Memory allocation failed”);
return 1;
}
for (int i = 0; i < 5; i++) {
ptr[i] = i;
}
printf("Array elements: ");
for (int i = 0; i < 5; i++) {
printf("%d ", ptr[i]);
}
printf("");
free(ptr);
return 0;
}
```
In this example, memory is allocated for an array of 5 integers using `malloc`. The allocated memory is then freed using `free`.
5. What is the difference between `for`, `while`, and `do-while` loops in C?
– `for` loop: The `for` loop is used when the number of iterations is known beforehand. It consists of an initialization, a condition, and an increment or decrement expression.
– `while` loop: The `while` loop is used when the number of iterations is not known beforehand. It checks the condition before entering the loop.
– `do-while` loop: The `do-while` loop is similar to the `while` loop, but it checks the condition after executing the loop body. This guarantees that the loop body is executed at least once.
Understanding these interview questions on C and their answers will help candidates demonstrate their knowledge and skills in the programming language during their interviews.