C Programming Interview Questions and Answers
Q: What are the basic data types in C?
A: C has the following basic data types:
- int - for integers
- float - for single-precision floating-point numbers
- double - for double-precision floating-point numbers
- char - for characters
- void - for no value or return type
Q: What is the difference between ++i and i++?
A: ++i is pre-increment: the value is incremented first, then used.
i++ is post-increment: the current value is used first, then incremented.
Q: What is a pointer in C?
A: A pointer is a variable that stores the memory address of another variable.
Example:
int a = 10;
int *p = &a;
Q: What is the difference between scanf() and gets()?
A: scanf() stops reading at whitespace.
gets() reads an entire line including spaces until a newline is encountered.
Note: gets() is unsafe and deprecated due to buffer overflow risks.
Q: What is the use of the sizeof() operator?
A: sizeof() is used to determine the memory size (in bytes) of a variable or data type.
Q: Explain the difference between call by value and call by reference.
A: Call by value passes a copy of the variable; the original remains unchanged.
Call by reference uses pointers to pass the address, allowing modification of the original variable.
Q: How are arrays and pointers related in C?
A: In C, the array name acts as a pointer to the first element.
Example: arr[0] is equivalent to *(arr + 0)
Q: What is a static variable?
A: A static variable retains its value between function calls and has internal linkage (limited scope
but permanent lifetime).
Q: What is a segmentation fault?
A: A segmentation fault occurs when a program tries to access memory it is not allowed to access,
often due to:
- Dereferencing NULL or uninitialized pointers
- Buffer overflows
Q: How is memory allocated dynamically in C?
A: Using standard library functions:
- malloc() - allocates uninitialized memory
- calloc() - allocates zero-initialized memory
- realloc() - resizes previously allocated memory
- free() - deallocates memory
Q: What is the difference between struct and union?
A: struct: All members have separate memory locations.
union: All members share the same memory space (the size of the largest member).
Q: What is a function pointer in C?
A: A function pointer stores the address of a function and can be used to call it.
Example:
void hello() { printf("Hello"); }
void (*func_ptr)() = hello;
func_ptr();
Q: What does the volatile keyword do?
A: volatile tells the compiler not to optimize the variable, as its value can change at any time (e.g., by
hardware or an interrupt).
Q: What is undefined behavior in C?
A: It refers to code operations that have no predictable result, like:
- Using uninitialized variables
- Buffer overflow
- Division by zero
Q: What is the difference between deep copy and shallow copy?
A: Shallow copy copies only the pointers, not the actual data.
Deep copy duplicates the actual data that the pointer points to.
Q: Write a function to swap two numbers using pointers.
A: #include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
int main() {
int x = 5, y = 10;
swap(&x, &y);
printf("x = %d, y = %d\n", x, y);
return 0;