C Interview Questions
C Interview Questions with Answers
This page gives simple C interview questions with short answers. The questions are made for beginners, so you can use them to study for tests and job interviews.
- Core C basics and program structure
- Variables and data types
- Conditions and loops
- Arrays and strings
- Pointers and memory
- Functions, scope, and structures
Core C Basics
1. What is C used for?
Answer:
- C is a programming language used to write fast and efficient programs.
- It is often used for system software, embedded systems, and performance-critical applications.
2. What is the basic structure of a C program?
Answer:
- A typical C program has
#includelines, amain()function, and C statements inside{ }.
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
3. What does the main() function do in C?
Answer:
main()is the starting point of every C program.- The code inside
main()runs first when the program starts. - The
return 0;line usually means "the program ended successfully".
4. What is the purpose of #include <stdio.h>?
Answer:
#include <stdio.h>tells the compiler to include the standard input/output library.- It lets you use functions like
printf()andscanf().
5. What does printf() do?
Answer:
printf()prints text and values to the screen.- You can use format specifiers like
%dfor integers and%ffor floating-point numbers.
#include <stdio.h>
int main() {
int age = 30;
printf("Age: %d\n", age);
return 0;
}
Variables and Data Types
6. What is a variable in C?
Answer:
- A variable is a named place in memory where a value is stored.
- In C, you must declare the type before the variable name.
int count = 10;
float price = 19.95;
char grade = 'A';
7. What is the difference between int, float, and char?
Answer:
intstores whole numbers (e.g. 3, -10, 200).floatstores numbers with decimals (e.g. 3.14, -0.5).charstores a single character (e.g.'A','B').
8. What is a constant and why would you use one?
Answer:
- A constant is a value that cannot be changed once it is set.
- Using
constmakes your intent clear and protects the value from being changed by mistake.
const int DAYS_IN_WEEK = 7;
// DAYS_IN_WEEK = 8; // not allowed
9. How do you read user input in C?
Answer:
- You can use
scanf()to read values from the keyboard. - You must pass the address of the variable using
&.
int number;
scanf("%d", &number);
Conditions and Loops
10. What is an if statement used for?
Answer:
- An
ifstatement lets the program make a decision. - Code inside the
ifblock runs only when the condition is true.
int age = 20;
if (age >= 18) {
printf("Adult\n");
} else {
printf("Not adult\n");
}
11. What is a loop in C?
Answer:
- A loop repeats the same block of code multiple times.
- C has
for,while, anddo...whileloops.
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
}
12. What is the difference between while and do...while?
Answer:
whilechecks the condition before running the loop body.do...whileruns the loop body once, then checks the condition.- This means
do...whilealways runs at least one time.
13. What is a switch statement?
Answer:
- A
switchstatement chooses one block of code to run based on the value of an expression. - Each option is written as a
case, anddefaulthandles all other values.
int day = 2;
switch (day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
default:
printf("Another day");
}
Arrays and Strings
14. What is an array in C?
Answer:
- An array stores many values of the same type in one variable name.
- You access each element by its index, starting from 0.
int scores[3] = {10, 20, 30};
printf("%d\n", scores[0]); // first element
15. How are strings represented in C?
Answer:
- In C, a string is an array of characters.
- It ends with a special character
'\0'(the null terminator).
char greeting[] = "Hello";
printf("%s\n", greeting);
Pointers and Memory
16. What is a pointer in C?
Answer:
- A pointer is a variable that stores the memory address of another variable.
- Pointers are used to work directly with memory and to share data with functions.
int value = 10;
int *ptr = &value; // ptr holds the address of value
17. What do the & and * operators mean with pointers?
Answer:
&gives the address of a variable (the "address of" operator).*gives the value at an address (the "dereference" operator).
int x = 5;
int *p = &x; // &x is the address of x
printf("%d\n", *p); // *p is the value at that address (5)
18. What is a NULL pointer?
Answer:
- A NULL pointer is a pointer that does not point to any valid memory location.
- It is often used to show that the pointer is "empty" or not yet set.
int *ptr = NULL; // pointer not pointing anywhere yet
Functions, Scope, and Structures
19. What is a function in C and why use it?
Answer:
- A function is a block of code that performs a specific task.
- Functions help you split a large program into smaller, reusable pieces.
int add(int a, int b) {
return a + b;
}
20. What is the difference between local and global variables?
Answer:
- A local variable is declared inside a function and can be used only there.
- A global variable is declared outside all functions and can be used in the entire file (after its declaration).
21. What is a struct in C?
Answer:
- A
struct(structure) is a custom type that groups related variables under one name. - The grouped variables can have different types.
struct Person {
char name[50];
int age;
};
22. Why would you use a struct instead of separate variables?
Answer:
- A struct keeps related information together, making the code easier to understand.
- It is useful when you want to treat multiple pieces of data as one unit (for example, a person or a product).