C Interview Questions
C Interview Questions with Answers
This page has common C interview questions, with short answers and code examples. The questions go from the basics to pointers and memory management, which almost every C interview includes.
Each answer links to the tutorial chapter where you can learn more. At the end of the page, you can test yourself with an interview quiz.
- C basics
- Data types and operators
- Conditions and loops
- Arrays and strings
- Pointers
- Functions
- Memory management
- Structures and more
C Basics
1. What does #include do?
Answer:
#includetells the preprocessor to copy the contents of a header file into your program, before it is compiled.- Use angle brackets for the standard library, and double quotes for your own header files:
#include <stdio.h> // A standard library header
#include "myfunctions.h" // Your own header file
Learn more: C Organize Code
2. What does main() return, and why?
Answer:
main()returns anintto the operating system when the program ends.0means that the program finished with no errors. Any other value means that something went wrong.
int main() {
printf("Hello World!");
return 0;
}
Learn more: C Syntax
3. What is the difference between a declaration and a definition?
Answer:
- A declaration tells the compiler that something exists, and what type it has.
- A definition creates it. For a function, the definition is the body with the code:
int add(int a, int b); // Declaration
int add(int a, int b) { // Definition
return a + b;
}
Learn more: C Function Declaration
Data Types and Operators
4. What are the basic data types in C?
Answer:
intfor whole numbers,floatanddoublefor decimal numbers, andcharfor single characters.- The size of each type depends on the system. On most computers,
intis 4 bytes,floatis 4 bytes,doubleis 8 bytes andcharis 1 byte.
Learn more: C Data Types
5. What is the difference between float and double?
Answer:
doubleis more precise. It stores about 15 digits correctly, whilefloatstores about 6-7 digits.doubleusually uses 8 bytes, andfloatuses 4. For most calculations,doubleis the safer choice.
Learn more: C Numbers
6. What is the difference between signed and unsigned?
Answer:
- A signed type can store negative and positive numbers. An unsigned type can only store zero and positive numbers.
- Because it does not need room for negative numbers, an unsigned type can store bigger positive numbers. For example, a 4-byte
unsigned intgoes from 0 to 4294967295.
Learn more: C Extended Types
7. What does sizeof do?
Answer:
sizeofgives the size of a type or a variable, in bytes.- It is an operator, not a function, and the size is usually known already when the program is compiled.
int numbers[5];
printf("%zu\n", sizeof(int)); // 4 (on most systems)
printf("%zu\n", sizeof(numbers)); // 20 (5 x 4 bytes)
Learn more: C Memory Size
8. What is the difference between i++ and ++i?
Answer:
- Both add 1 to
i. The difference shows when you use the value in the same statement. i++(postfix) uses the current value first, and then adds 1.++i(prefix) adds 1 first, and then uses the new value:
int i = 5;
int a = i++; // a is 5, i is 6
int j = 5;
int b = ++j; // b is 6, j is 6
Learn more: C Arithmetic Operators
9. What does this code print?
printf("%d\n", 7 / 2);
Answer:
- It prints
3, not3.5. When you divide two integers, the result is also an integer, and the decimals are removed. - To get
3.5, use at least one decimal number:7.0 / 2, and print it with%f.
Learn more: C Arithmetic Operators
10. What is the difference between = and ==?
Answer:
=gives a variable a value.==checks if two values are equal.- Mixing them up is a common bug.
if (x = 5)changesxto 5, and the condition is always true.
Learn more: C Comparison Operators
11. What is the difference between const and #define?
Answer:
constcreates a variable that cannot be changed. It has a type, and the compiler checks how you use it.#definecreates a macro. The preprocessor replaces the name with the value everywhere in the code, before compiling. It has no type.
const int MAX_USERS = 100;
#define MAX_SCORE 100
Learn more: C Constants and C Macros
Conditions and Loops
12. What is the difference between while and do...while?
Answer:
- A
whileloop checks the condition first, so it may not run at all. - A
do...whileloop runs the code first, and checks the condition after. So it always runs at least one time.
Learn more: C Do/While Loop
13. What is the difference between break and continue?
Answer:
breakstops the loop completely.continueskips the rest of the current round, and goes on with the next one.
Learn more: C Break and Continue
14. What happens if you forget break in a switch?
Answer:
- The program goes on and runs the code in the next
casetoo. This is called fall-through:
int day = 1;
switch (day) {
case 1:
printf("Monday\n");
case 2:
printf("Tuesday\n");
break;
}
// Prints both Monday and Tuesday
Learn more: C Switch
Arrays and Strings
15. How are strings stored in C?
Answer:
- C has no string type. A string is an array of characters, which ends with the null character
\0. - That is why
"Hello"uses 6 characters of memory, not 5.
Learn more: C The Null Character
16. What is the difference between sizeof and strlen()?
Answer:
sizeofgives the size of the whole array, in bytes.strlen()counts the characters in the string, until the\0:
char name[20] = "Liam";
printf("%zu\n", sizeof(name)); // 20
printf("%zu\n", strlen(name)); // 4
Learn more: C String Functions
17. Why can you not compare strings with ==?
Answer:
==compares the addresses of the two strings in memory, not the characters in them.- Use
strcmp()instead. It returns0when the two strings are equal:
char a[] = "Hello";
char b[] = "Hello";
if (strcmp(a, b) == 0) {
printf("The strings are equal\n");
}
Learn more: C String Functions
18. How do you find the number of elements in an array?
Answer:
- Divide the size of the whole array by the size of one element:
int numbers[] = {10, 20, 30, 40};
int length = sizeof(numbers) / sizeof(numbers[0]); // 4
- This does not work inside a function that gets the array as a parameter, because the function only gets a pointer to the array. That is why you often pass the length as an extra parameter.
Learn more: C Array Size
19. What happens if you use an index outside an array?
Answer:
- C does not check the index. The program reads or writes memory that does not belong to the array.
- This is undefined behavior: the program may print a wrong value, change other variables, or crash. Writing outside an array is called a buffer overflow.
Learn more: C Memory Mistakes
Pointers
20. What is a pointer?
Answer:
- A pointer is a variable that stores the memory address of another variable.
&gives the address of a variable, and*gives the value at an address (this is called dereferencing):
int age = 20;
int *ptr = &age; // ptr stores the address of age
printf("%d\n", *ptr); // 20
Learn more: C Pointers
21. What is a NULL pointer, and why should you check for it?
Answer:
- A
NULLpointer does not point to anything. - Functions like
malloc()andfopen()returnNULLwhen they fail. Using aNULLpointer to read or change a value crashes the program, so always check first:
int *ptr = malloc(sizeof(*ptr));
if (ptr == NULL) {
printf("Not enough memory\n");
return 1;
}
Learn more: C NULL
22. How are arrays and pointers related?
Answer:
- In most cases, the name of an array works as a pointer to its first element.
numbers[i]is the same as*(numbers + i). - But an array is not a pointer:
sizeofgives the size of the whole array, and you cannot give an array a new address.
Learn more: C Pointers and Arrays
23. What is pointer arithmetic?
Answer:
- When you add 1 to a pointer, it moves to the next element, not to the next byte. For an
intpointer, that is usually 4 bytes further:
int numbers[3] = {10, 20, 30};
int *ptr = numbers;
printf("%d\n", *(ptr + 1)); // 20
Learn more: C Pointer Arithmetic
24. What is a void pointer?
Answer:
- A
void *pointer can store the address of any type. For example,malloc()returns avoid *. - You cannot use
*on avoid *directly, because C does not know what type it points to. Store it in a pointer of the right type first.
25. What is a function pointer?
Answer:
- A function pointer stores the address of a function, so you can call the function through the pointer.
- It is often used for callbacks, where you pass a function to another function (like
qsort()):
int add(int a, int b) {
return a + b;
}
int (*operation)(int, int) = add;
printf("%d\n", operation(2, 3)); // 5
Learn more: C Function Pointers
Functions
26. What is the difference between pass by value and pass by reference?
Answer:
- C always passes a copy of the value to a function (pass by value). If the function changes the copy, the original variable does not change.
- To let a function change a variable, pass its address instead. This is called pass by reference:
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int x = 1;
int y = 2;
swap(&x, &y); // Now x is 2 and y is 1
Learn more: C Pass By Reference
27. What is recursion?
Answer:
- Recursion is when a function calls itself.
- A recursive function needs a base case, a condition where it stops calling itself. Without it, the function never stops, and the program crashes:
int factorial(int n) {
if (n <= 1) {
return 1; // Base case
}
return n * factorial(n - 1);
}
Learn more: C Recursion
28. What is the difference between local and global variables?
Answer:
- A local variable is created inside a function. It can only be used there, and it is removed when the function ends.
- A global variable is created outside all functions. It can be used everywhere in the file, and it exists while the program runs.
Learn more: C Scope
29. What does the static keyword do?
Answer:
- For a local variable,
statickeeps the value between function calls:
void count() {
static int calls = 0;
calls++;
printf("%d\n", calls);
}
// Calling count() three times prints 1, 2 and 3
- For a global variable or a function,
staticmeans that it can only be used in the file where it is created.
Learn more: C Storage Classes
Memory Management
30. What is the difference between malloc() and calloc()?
Answer:
malloc()takes the number of bytes to allocate. The memory is not cleared, so it can contain random values.calloc()takes the number of elements and the size of each element, and sets all the memory to zero:
int *a = malloc(5 * sizeof(int)); // 5 ints, random values
int *b = calloc(5, sizeof(int)); // 5 ints, all set to 0
Learn more: C Allocate Memory
31. What does realloc() do?
Answer:
realloc()changes the size of memory that you allocated before. It keeps the old values, but it may move them to a new address.- If it fails, it returns
NULL, and the old memory is not freed. So store the result in a new pointer first, and check it forNULL.
Learn more: C Reallocate Memory
32. What is a memory leak?
Answer:
- A memory leak happens when you allocate memory, but never free it. For example, when the pointer to the memory is lost before you call
free(). - The program then uses more and more memory, which can make it slow, or make it run out of memory.
Learn more: C Deallocate Memory
33. What is a dangling pointer?
Answer:
- A dangling pointer points to memory that is no longer valid, for example after
free(), or to a local variable of a function that has ended. - To avoid it, set the pointer to
NULLafter you free the memory:
free(ptr);
ptr = NULL;
Learn more: C Memory Mistakes
34. What is undefined behavior?
Answer:
- Undefined behavior is code where the C language does not say what should happen. The result cannot be predicted: the program may seem to work, print wrong values, or crash.
- Examples are using an index outside an array, using memory after
free(), and dividing an integer by zero.
Learn more: C Memory Mistakes
Structures and More
35. What is the difference between a struct and a union?
Answer:
- In a struct, every member has its own memory, so you can use all of them at the same time.
- In a union, all members share the same memory. The union is as big as its biggest member, and only one member can hold a value at a time.
Learn more: C Unions
36. What is the difference between . and ->?
Answer:
- Use
.to access a member of a struct variable. Use->to access a member through a pointer to a struct:
struct Person p1 = {"Liam", 20};
struct Person *ptr = &p1;
printf("%d\n", p1.age); // 20
printf("%d\n", ptr->age); // 20, the same as (*ptr).age
Learn more: C Structs and Pointers
37. What is typedef used for?
Answer:
typedefgives a type a new name. It is often used to make struct types shorter to write:
typedef struct {
int x;
int y;
} Point;
Point p1 = {3, 4}; // No need to write struct
Learn more: C typedef
38. What are header guards?
Answer:
- Header guards stop a header file from being included more than once in the same file, which would cause errors:
#ifndef MYFUNCTIONS_H
#define MYFUNCTIONS_H
int add(int a, int b);
#endif
Learn more: C Organize Code
39. What is the difference between a macro and a function?
Answer:
- A macro is text that the preprocessor replaces before compiling. It has no type checking, and it can give unexpected results.
- A function is real code, and the compiler checks the types of its parameters:
#define SQUARE(x) x * x
printf("%d\n", SQUARE(2 + 3)); // 11, not 25: 2 + 3 * 2 + 3
- To avoid this, put parentheses around the parameters in a macro:
((x) * (x)).
Learn more: C Macros
Test Yourself: Interview Quiz
Ready for the interview? Choose your answer to each question, and read the explanation:
Question
Code
Explanation
Prepare for Your Interview
- Test yourself with our C Exercises and C Quiz
- Solve C Practice Problems, and see how many test cases your code passes
- Show what you know with the C Certificate