Top Interview Questions and Answers on C ( 2025 )
Some C interview questions, along with their explanations and example answers. This covers a range of topics, from basics to more advanced concepts.
Answer: Both ++i and i++ are increment operators, but they differ in their behavior:
++i is pre-increment. It increments the value of i and then uses it.
i++ is post-increment. It uses the current value of i and then increments it.
Example:
int i = 5;
int a = ++i; // a = 6, i = 6
int b = i++; // b = 6, i = 7
Answer: A pointer is a variable that stores the memory address of another variable. It allows for indirect access to the value stored in another variable.
Example:
int x = 10;
int *p = &x; // p holds the address of x
printf("Value of x: %d\n", *p); // Dereferencing p to get the value of x
Answer: A NULL pointer is a pointer that doesn’t point to any valid memory location. It is often used to indicate that the pointer is not initialized or doesn't point to any valid object.
Example:
int *ptr = NULL; // NULL pointer
if (ptr == NULL) {
printf("Pointer is NULL\n");
}
Answer:
malloc() (memory allocation) allocates a specified number of bytes of memory but does not initialize the memory. The initial values are undetermined.
calloc() (contiguous allocation) allocates memory for an array of specified elements and initializes all bits to zero.
Example:
int *arr1 = (int*)malloc(5 * sizeof(int)); // Allocates memory for 5 integers
int *arr2 = (int*)calloc(5, sizeof(int)); // Allocates and initializes memory for 5 integers to 0
Answer: free() is used to deallocate memory that was previously allocated by functions like malloc(), calloc(), or realloc(). It prevents memory leaks by releasing memory back to the heap.
Example:
int *ptr = (int*)malloc(10 * sizeof(int)); // Allocate memory
free(ptr); // Free the allocated memory
Answer: A structure in C is a user-defined data type that allows grouping of different types of variables (members) under a single name.
Example:
struct Person {
char name[50];
int age;
};
struct Person person1;
person1.age = 30;
strcpy(person1.name, "John");
Answer: typedef is used to create a new name (alias) for an existing data type. It improves code readability and allows for abstraction.
Example:
typedef unsigned int uint;
uint x = 10; // x is now of type unsigned int
Answer:
break is used to terminate a loop or switch statement prematurely.
continue is used to skip the current iteration of the loop and move to the next iteration.
Example:
for (int i = 0; i < 5; i++) {
if (i == 3) {
continue; // Skip when i equals 3
}
printf("%d ", i); // Prints 0 1 2 4
}
Answer:
== is the equality operator, used to compare two values.
= is the assignment operator, used to assign a value to a variable.
Example:
int a = 5; // Assignment
if (a == 5) { // Equality check
printf("a is 5\n");
}
Answer: In C, there are four storage classes:
auto: Default storage class for local variables.
register: Suggests that the variable be stored in a CPU register for faster access.
static: Preserves the value of a variable between function calls.
extern: Declares a variable that is defined outside the current function or file.
Example:
static int counter = 0; // Retains value between function calls
extern int x; // Defined elsewhere in the program
Answer: The sizeof() operator returns the size (in bytes) of a data type or object.
Example:
int x = 10;
printf("Size of x: %zu bytes\n", sizeof(x)); // Output will be the size of an int
Answer: The goto statement is used to transfer control to a different part of the program. It is generally discouraged as it makes code harder to follow and maintain.
Example:
int x = 5;
if (x > 3) {
goto label; // Jump to label
}
label:
printf("x is greater than 3\n");
Answer: Dividing an integer by zero results in undefined behavior. In most cases, it will cause a runtime error or the program may crash.
Example:
int a = 5;
int b = 0;
int result = a / b; // Undefined behavior, division by zero
Answer:
strcpy() copies a string to another string, and it does not check for buffer overflow.
strncpy() copies a specified number of characters and ensures that no more than the given number of characters are copied.
Example:
char dest[10];
strcpy(dest, "Hello"); // Copies "Hello" to dest
strncpy(dest, "HelloWorld", 5); // Copies only "Hello" to dest
Answer: A segmentation fault (segfault) occurs when a program attempts to access a memory location that it's not allowed to. Common causes include dereferencing NULL pointers, accessing out-of-bounds arrays, or improper memory management.
Example:
int *ptr = NULL;
*ptr = 10; // Dereferencing a NULL pointer causes a segmentation fault
Answer: An enum (enumeration) is a user-defined data type that consists of integral constants, making the code more readable.
Example:
enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
enum Day today = Wednesday;
Answer:
A struct is a collection of variables of different data types, each of which has its own memory location.
A union is a collection of variables of different data types, but all members share the same memory location, meaning only one member can hold a value at any given time.
Example:
struct Student {
int age;
char name[50];
};
union Data {
int intValue;
char charValue;
};
These questions and answers cover essential C programming concepts and should help in preparing for C programming interviews.