C Interview Questions for Freshers
Most campus placement and fresher technical interviews start with C. Interviewers don't expect you to know everything; they check whether your basics are clear and whether you can explain them in simple words. This page collects the questions asked most often, with short answers you can say in an interview, and links to the full lessons.
The questions are grouped by topic: basics, data types and operators, control flow, functions, arrays and strings, pointers, memory, structures and files. Read the answer, then try saying it in your own words.
What You Will Learn
- The 40 most common C interview questions for freshers.
- Short, interview-ready answers for each.
- Small programs that prove the tricky answers.
- How to answer when you don't know something.
- Which topics to revise first before an interview.
- Mistakes that make a good answer sound weak.
C Interview Questions for Freshers Explained
How to answer: Use the D-E-E pattern: one-line Definition, a tiny Example, and one Extra point (a use, a difference or a pitfall). For example: "A pointer is a variable that stores an address (definition). int *p = &x; stores x's address (example). It is used for dynamic memory and call by reference (extra)." Short, clear answers beat long memorised paragraphs.
Memory trick: Interviews follow the order of a textbook: basics → data types → operators → loops → functions → arrays → strings → pointers → structures → memory → files. Revise in that order, and you'll recognise where every question comes from.
C Basics
- 1. What is C?
- C is a general-purpose, procedural, middle-level programming language developed by Dennis Ritchie at Bell Labs in 1972. It is fast, portable and close to the hardware, so it is used for operating systems, embedded systems and compilers.
- 2. Why is C called a middle-level language?
- It has high-level features (functions, loops, readable syntax) and low-level features (pointers, bit manipulation, direct memory access).
- 3. What are the main features of C?
- Simple, fast, portable, structured (functions), rich library, pointers and dynamic memory, extensible.
- 4. What are the steps from source code to a running program?
- Preprocessing → compilation → assembly → linking, then the loader runs the executable. See Compilation Process in C.
- 5. What is the difference between a compiler and an interpreter?
- A compiler translates the whole program into machine code before running (C); an interpreter translates and runs line by line (e.g. Python).
- 6. What is the role of main()?
- It is the entry point: execution starts there. It returns an int to the operating system; 0 means success.
Data Types, Variables and Operators
- 7. What are the basic data types in C?
- int, char, float, double and void, with modifiers short, long, signed and unsigned.
- 8. What is the size of int?
- It depends on the compiler and system; it is usually 4 bytes on modern 32/64-bit systems. Use
sizeof(int)to check. - 9. What is the difference between a local and a global variable?
- A local variable is declared inside a function and exists only there; a global variable is declared outside all functions and is visible to the whole file (and others with extern).
- 10. What are storage classes?
- auto, register, static, extern. They decide a variable's scope, lifetime, default value and storage location.
- 11. What is a static variable?
- A variable that keeps its value between function calls; it is initialised only once (default 0).
- 12. What is the difference between = and ==?
=assigns a value;==compares two values and gives 1 or 0.- 13. What is the difference between i++ and ++i?
i++(post-increment) uses the old value, then increases;++i(pre-increment) increases first, then uses the new value.- 14. What is typecasting?
- Converting a value from one type to another, e.g.
(float)a / bto avoid integer division.
Example 1: Proving Three Common Answers
#include <stdio.h>
void counter(void)
{
static int calls = 0; /* keeps its value between calls */
calls++;
printf("counter called %d time(s)\n", calls);
}
int main(void)
{
int i = 5, a, b;
int x = 7, y = 2;
a = i++; /* a gets 5, then i becomes 6 */
b = ++i; /* i becomes 7, then b gets 7 */
printf("a = %d, b = %d, i = %d\n", a, b, i);
printf("7 / 2 = %d, (float)7 / 2 = %.1f\n", x / y, (float) x / y);
counter();
counter();
counter();
return 0;
}
Output
a = 5, b = 7, i = 7 7 / 2 = 3, (float)7 / 2 = 3.5 counter called 1 time(s) counter called 2 time(s) counter called 3 time(s)
Code Explanation
a = i++stores the old value 5;b = ++iincrements first and stores 7.7 / 2with ints gives 3 (the fraction is dropped); casting one operand to float gives 3.5.static int callsis created once and remembers its value, so the count goes 1, 2, 3. A normal local variable would print 1 every time.
Control Flow and Functions
- 15. What is the difference between while and do-while?
whilechecks the condition first (may run 0 times);do-whileruns the body first, so it runs at least once.- 16. What is the difference between break and continue?
breakexits the loop or switch;continueskips the rest of the current iteration and goes to the next.- 17. Can switch work with float or strings?
- No. The switch expression must be an integer type (int, char, enum).
- 18. What is a function prototype?
- A declaration that tells the compiler a function's name, return type and parameters before it is used, e.g.
int add(int, int); - 19. What is call by value and call by reference?
- Call by value passes a copy (changes don't affect the caller); call by reference passes an address (pointer), so the function can change the caller's variable.
- 20. What is recursion?
- A function calling itself, with a base case that stops the calls, e.g. factorial(n) = n × factorial(n − 1).
Arrays, Strings and Pointers
- 21. What is an array?
- A collection of elements of the same type stored in contiguous memory, accessed by index starting at 0.
- 22. What is a string in C?
- A character array ending with the null character
'\0'. - 23. What is the difference between strlen and sizeof for a string?
strlencounts characters before'\0';sizeofgives the total bytes of the array, including'\0'and unused space.- 24. What is a pointer?
- A variable that stores the address of another variable.
&gives an address,*gives the value at an address. - 25. What is a NULL pointer?
- A pointer that points to nothing (value
NULL); used to mark "no valid address". Dereferencing it is an error. - 26. What is a dangling pointer?
- A pointer that still holds the address of memory that has been freed or gone out of scope.
- 27. What is a void pointer?
- A generic pointer (
void *) that can hold the address of any type; it must be cast before dereferencing. malloc returns one. - 28. How are arrays and pointers related?
- An array name usually converts to a pointer to its first element, so
a[i]is the same as*(a + i). But an array is not a pointer: you can't assign to it, and sizeof differs.
Example 2: strlen vs sizeof and Call by Reference
#include <stdio.h>
#include <string.h>
void swap(int *x, int *y) /* call by reference */
{
int t = *x;
*x = *y;
*y = t;
}
int main(void)
{
char name[20] = "Ravi";
int a = 10, b = 20;
int arr[3] = {5, 6, 7};
printf("strlen = %zu, sizeof = %zu\n", strlen(name), sizeof name);
swap(&a, &b);
printf("after swap: a = %d, b = %d\n", a, b);
printf("arr[2] = %d, *(arr + 2) = %d\n", arr[2], *(arr + 2));
return 0;
}
Output
strlen = 4, sizeof = 20 after swap: a = 20, b = 10 arr[2] = 7, *(arr + 2) = 7
strlencounts "Ravi" = 4;sizeofis the whole 20-byte array.swapreceives addresses, so it changes main'saandb.arr[2]and*(arr + 2)are the same element.
Memory, Structures and Files
- 29. What is dynamic memory allocation?
- Allocating memory at run time from the heap with malloc, calloc, realloc and releasing it with free.
- 30. What is the difference between malloc and calloc?
- malloc(size) allocates one block with garbage values; calloc(n, size) allocates n elements and sets them to zero.
- 31. What is a memory leak?
- Memory allocated with malloc that is never freed and can no longer be reached, so it is wasted until the program ends.
- 32. What is the difference between stack and heap?
- The stack holds local variables and function calls (automatic, fast, limited); the heap holds dynamic memory (manual malloc/free, larger).
- 33. What is a structure?
- A user-defined type that groups variables of different types under one name, e.g. a student's roll, name and marks.
- 34. What is the difference between structure and union?
- A structure gives each member its own memory (size ≥ sum of members); a union's members share one memory area (size = largest member), so only one member holds a value at a time.
- 35. What is typedef?
- A keyword that gives a new name to an existing type, e.g.
typedef struct student Student; - 36. What are the file opening modes?
"r"read,"w"write (creates/erases),"a"append, plus"+"for read and write and"b"for binary.- 37. What is the difference between a text file and a binary file?
- A text file stores readable characters; a binary file stores the raw bytes of data, which is compact and supports fixed-size records.
- 38. What is a macro?
- A name defined with
#definethat the preprocessor replaces with its text before compilation. - 39. What are command line arguments?
- Values passed when running a program, received as
int argc(count) andchar *argv[](strings) in main. - 40. What is the difference between C and C++?
- C is procedural; C++ adds object-oriented features (classes, inheritance, polymorphism), references, function overloading and the STL.
Important Points to Remember
- Answer with definition + example + one extra point.
- Say "it depends on the compiler" for sizes like sizeof(int), then give the usual value.
- Know the classic differences: = vs ==, i++ vs ++i, while vs do-while, malloc vs calloc, structure vs union, stack vs heap.
- Be ready to write small programs: swap, factorial, prime, reverse a number/string, Fibonacci.
- If you don't know, say so honestly and explain how you would find out; never bluff.
- Revise pointers most: they decide most C interviews.
Common Mistakes Beginners Make
| Mistake in an interview | Better answer |
|---|---|
| "int is always 2 bytes" | "It depends on the system; usually 4 bytes today." |
| "An array is a pointer" | "An array name decays to a pointer to its first element, but they are different." |
| Reciting long definitions | One clear line and an example |
| Saying "C is object-oriented" | C is procedural; C++ is object-oriented |
| Guessing when unsure | "I'm not sure, but I think… I'd verify it with sizeof / a small program." |
Interview Practice Questions
- Explain the difference between call by value and call by reference with an example.
- What happens if you forget to free memory allocated with malloc?
- Why does
printf("%d", 7/2)print 3? - What is the difference between a structure and a union? Which is bigger?
- What is a dangling pointer, and how do you avoid it?
Show answers
- Call by value copies the argument, so changes stay inside the function; call by reference passes the address, e.g.
swap(&a, &b)changes the caller's variables. - It causes a memory leak: the memory stays reserved until the program ends.
- Both operands are int, so integer division drops the fractional part.
- Structure members have separate memory; union members share memory. A structure is at least as big as the sum of its members; a union is as big as its largest member.
- A pointer to freed or out-of-scope memory; set it to NULL after free and never return the address of a local variable.
Coding Exercise
A favourite fresher question: "Write a program to check whether a number is a palindrome." Write int is_palindrome(int n) and test it with 121, 1331 and 123.
Show solution
#include <stdio.h>
int is_palindrome(int n)
{
int original = n, reversed = 0;
while (n > 0)
{
reversed = reversed * 10 + n % 10;
n /= 10;
}
return reversed == original;
}
int main(void)
{
int tests[] = {121, 1331, 123};
int i;
for (i = 0; i < 3; i++)
printf("%d is %sa palindrome\n", tests[i], is_palindrome(tests[i]) ? "" : "not ");
return 0;
}
121 is a palindrome 1331 is a palindrome 123 is not a palindrome
Explain your logic aloud while writing: "I reverse the digits using % 10 and / 10, then compare with the original."
Quick Quiz: C Interview Basics (MCQ)
-
Who developed the C language?
A) James Gosling B) Dennis Ritchie C) Bjarne Stroustrup D) Ken Thompson alone
-
Which loop runs at least once?
A) for B) while C) do-while D) None
-
Which function allocates memory and sets it to zero?
A) malloc B) calloc C) realloc D) free
-
A string in C ends with:
A) '\n' B) '\0' C) '.' D) EOF
-
The size of a union equals:
A) Sum of members B) Size of its largest member (plus padding) C) Size of its first member D) 4 bytes
Show answers
- B: Dennis Ritchie.
- C: do-while.
- B: calloc.
- B: '\0'.
- B: the size of its largest member (plus padding).
Quick Revision for Exams and Interviews
Read this before your interview. The highlighted words are the answer.
Must-remember facts
| C | Dennis Ritchie, 1972, procedural, middle-level |
|---|---|
| Build steps | Preprocess → compile → assemble → link |
| i++ / ++i | Use then increase / increase then use |
| static local | Keeps value between calls |
| Pointer | Stores an address |
| malloc / calloc | Garbage / zeroed |
| struct / union | Separate / shared memory |
Frequently asked questions
- Which C topics are asked most in fresher interviews?
- Pointers, arrays and strings, storage classes, malloc/free, structures vs unions and small programs.
- How should I answer a C interview question?
- Give a one-line definition, a small example and one extra point; keep it short and clear.
- What if I don't know the answer?
- Say so honestly, share what you do know and how you would find out; interviewers value honesty over guessing.
Remember
- D-E-E: Definition, Example, Extra point.
- Revise in textbook order; spend the most time on pointers.
Next Topic
Next lesson: Pointer Interview Questions in C. You will practise the pointer questions that decide most C interviews.