C Programming Viva Questions
In a C lab exam, the external examiner looks at your program and then asks a few quick questions: "What is a header file?", "Why did you use a for loop here?", "What does & mean in scanf?". Viva answers must be short and confident, usually one or two lines.
This page gives 50 common C viva questions with crisp answers, grouped by topic, plus tips on explaining your own program. Practise saying the answers aloud, and you'll walk into the lab calm.
What You Will Learn
- 50 frequently asked C viva questions with one-line answers.
- How to explain your own lab program to the examiner.
- Small programs that prove key viva answers.
- Common wrong answers and how to correct them.
- A last-minute revision table.
C Programming Viva Questions Explained
Viva tip: Examiners usually start with your program: what it does, which header files it needs, why you chose a loop or function, and what happens for special inputs (0, negative numbers). Then they ask 3–5 theory questions. Answer in one sentence, add an example if they look unsure, and never argue.
Memory trick: For every program you submit, prepare "3 W's": What it does, Which concepts it uses (loop, array, function…), and What if (what happens with 0, negative or large input).
Basics (1–10)
- 1. Who developed C and when?
- Dennis Ritchie, at Bell Labs, in 1972.
- 2. What is a header file?
- A .h file containing declarations of library functions and macros, included with #include, e.g. stdio.h for printf.
- 3. What does #include <stdio.h> do?
- It tells the preprocessor to insert the standard input/output declarations (printf, scanf) into the program.
- 4. Why is main() needed?
- Program execution starts from main().
- 5. What does return 0 mean?
- The program ended successfully; 0 is returned to the operating system.
- 6. What is a compiler?
- A program that translates C source code into machine code (e.g. gcc).
- 7. What is the difference between a syntax error and a logical error?
- A syntax error breaks the language rules and stops compilation; a logical error compiles but gives a wrong result.
- 8. What is a keyword?
- A reserved word with a fixed meaning (int, if, while, return); it can't be used as a variable name. C has 32 keywords in C89.
- 9. What is an identifier?
- A name given to a variable, function or array, made of letters, digits and underscores, not starting with a digit.
- 10. What are comments?
- Notes ignored by the compiler: // single line, /* multi-line */.
Data Types, Input/Output and Operators (11–20)
- 11. Name the basic data types.
- int, char, float, double (and void).
- 12. What is the format specifier for int, float, char and string?
- %d, %f, %c, %s.
- 13. Why do we use & in scanf?
- scanf needs the address of the variable so it can store the input there.
- 14. Why is & not used for strings in scanf?
- An array name already gives the address of its first element.
- 15. What is a variable?
- A named memory location whose value can change.
- 16. What is a constant?
- A value that doesn't change, defined with const or #define.
- 17. What is the modulus operator?
- % gives the remainder of integer division, e.g. 7 % 3 = 1.
- 18. Difference between = and ==?
- = assigns, == compares.
- 19. What are logical operators?
- && (AND), || (OR), ! (NOT); they combine conditions.
- 20. What is the ternary operator?
- condition ? value1 : value2, a short if-else.
Example 1: Why & Is Needed in scanf
#include <stdio.h>
int main(void)
{
int age;
char name[20];
printf("Enter age: ");
scanf("%d", &age); /* &age = address of age */
printf("Enter name: ");
scanf("%19s", name); /* name is already an address */
printf("%s is %d years old.\n", name, age);
printf("Next year %s will be %d.\n", name, age + 1);
return 0;
}
Output
Enter age: 25 Enter name: Riya Riya is 25 years old. Next year Riya will be 26.
Code Explanation
scanf("%d", &age)receives the address ofage, so it can write the typed number into it. Without&, scanf would write to a random address.nameis an array; its name converts to the address of its first character, so no&is needed.%19slimits input to 19 characters, leaving room for'\0'. Examiners like it when you mention this.
Control Flow and Loops (21–28)
- 21. What is the difference between if-else and switch?
- if-else works with any condition; switch compares one integer/char expression with constant cases.
- 22. Why is break used in switch?
- To stop execution from falling into the next case.
- 23. Name the loops in C.
- for, while and do-while.
- 24. Which loop is entry-controlled and which is exit-controlled?
- for and while are entry-controlled; do-while is exit-controlled (runs at least once).
- 25. When do you use a for loop?
- When the number of iterations is known in advance.
- 26. What is an infinite loop?
- A loop whose condition never becomes false, e.g. while(1); stopped with break.
- 27. What does continue do?
- Skips the rest of the current iteration and starts the next one.
- 28. What is a nested loop?
- A loop inside another loop, e.g. for printing patterns or matrices.
Arrays, Strings and Functions (29–38)
- 29. What is an array?
- A collection of same-type elements in contiguous memory, indexed from 0.
- 30. What is the index of the last element of int a[10]?
- 9.
- 31. What is a 2D array?
- An array of arrays, like a table with rows and columns: int m[3][4].
- 32. What is a string?
- A char array ending with '\0'.
- 33. Name four string functions.
- strlen, strcpy, strcat, strcmp (from string.h).
- 34. What does strcmp return?
- 0 if equal, a negative value if the first string is smaller, positive if larger.
- 35. What is a function?
- A named block of code that performs a task and can be called many times.
- 36. What are actual and formal parameters?
- Actual parameters are the values passed in the call; formal parameters are the variables in the function definition that receive them.
- 37. What is recursion?
- A function calling itself, with a base case to stop.
- 38. What is the difference between library and user-defined functions?
- Library functions come with C (printf, sqrt); user-defined functions are written by the programmer.
Pointers, Structures, Memory and Files (39–50)
- 39. What is a pointer?
- A variable that stores the address of another variable.
- 40. What is the use of * and &?
- & gives an address; * gives the value at an address.
- 41. What is a NULL pointer?
- A pointer that points to nothing (NULL).
- 42. What is call by reference?
- Passing the address of a variable so the function can change the original.
- 43. What is a structure?
- A user-defined type grouping different data types, e.g. struct student.
- 44. How do you access structure members?
- With . for a variable (s.marks) and -> for a pointer (p->marks).
- 45. Difference between structure and union?
- Structure members have separate memory; union members share the same memory.
- 46. What is malloc?
- A function that allocates memory at run time and returns its address (stdlib.h).
- 47. Why do we use free()?
- To release memory allocated with malloc and avoid memory leaks.
- 48. What is a file pointer?
- A FILE * returned by fopen, used to read or write a file.
- 49. Name the file opening modes.
- "r", "w", "a", and with + for read/write, b for binary.
- 50. Why should we close a file?
- fclose saves buffered data to disk and frees the file resources.
Example 2: Structure, Pointer and ->
#include <stdio.h>
#include <string.h>
struct student
{
int roll;
char name[20];
float marks;
};
void add_grace(struct student *p, float g) /* call by reference */
{
p->marks += g;
}
int main(void)
{
struct student s = {12, "Aman", 38.0f};
printf("Before: %s (roll %d) has %.1f marks\n", s.name, s.roll, s.marks);
add_grace(&s, 2.0f);
printf("After : %s has %.1f marks\n", s.name, s.marks);
printf("Size of struct student = %zu bytes\n", sizeof s);
return 0;
}
Output
Before: Aman (roll 12) has 38.0 marks After : Aman has 40.0 marks Size of struct student = 28 bytes
s.marksuses the dot operator on a structure variable;p->marksuses the arrow on a pointer.p->marksmeans(*p).marks.- Passing
&slets the function change the original structure (call by reference). - The size is 4 + 20 + 4 = 28 bytes here; with other member orders, compilers may add padding.
Important Points to Remember
- Viva answers: one or two sentences, then an example if asked.
- Know your own program: what, which concepts, what if.
- Always mention the header file each function needs.
- Be clear on classic pairs: = vs ==, while vs do-while, struct vs union, call by value vs reference.
- If unsure, say "I think…" and explain your reasoning; don't stay silent.
- Keep your program indented and commented; examiners notice.
Common Mistakes Beginners Make
| Weak viva answer | Strong viva answer |
|---|---|
| "& is used in scanf because it is a rule" | "scanf needs the variable's address to store the input" |
| "stdio.h is a library" | "It is a header file with declarations of input/output functions" |
| "Array starts from 1" | "Array indexes start from 0" |
| "do-while and while are the same" | "do-while runs at least once because it checks the condition at the end" |
| Long memorised paragraphs | One clear sentence and a small example |
Viva Practice Questions
- Explain what your factorial program does and what happens if the user enters 0.
- Why is
getch()orconio.hnot used in modern C programs? - What is the difference between
printfandputs? - What is
'\n'? - What is the default value of a local variable?
Show answers
- It multiplies numbers from 1 to n; for 0 the loop doesn't run and the result stays 1, which is correct because 0! = 1.
- conio.h is a non-standard, old DOS/Turbo C header; standard C programs don't need it.
- printf prints formatted output using format specifiers; puts prints a string and adds a newline automatically.
- The newline escape sequence; it moves the cursor to the next line.
- It has no default value: it contains garbage until initialised (static and global variables default to 0).
Coding Exercise
Examiners often ask you to modify your program on the spot. Change a "sum of n numbers" program to print the sum of only the even numbers from 1 to n, for n = 10.
Show solution
#include <stdio.h>
int main(void)
{
int n = 10, i, sum = 0;
for (i = 1; i <= n; i++)
if (i % 2 == 0)
sum += i;
printf("Sum of even numbers from 1 to %d = %d\n", n, sum);
return 0;
}
Sum of even numbers from 1 to 10 = 30
Explain the change in one line: "I added a condition i % 2 == 0 so only even numbers are added." (You could also start at 2 and step by 2.)
Quick Quiz: C Viva Questions (MCQ)
-
Which header file is needed for
strlen()?A) stdio.h B) string.h C) stdlib.h D) math.h
-
Which loop is exit-controlled?
A) for B) while C) do-while D) None
-
To access a member through a structure pointer, use:
A) . B) -> C) * only D) &
-
strcmp("abc", "abc")returns:A) 1 B) 0 C) −1 D) 3
-
The default value of a static int variable is:
A) Garbage B) 0 C) 1 D) −1
Show answers
- B: string.h.
- C: do-while.
- B: ->.
- B: 0.
- B: 0.
Quick Revision for Exams and Interviews
Read this before your viva. The highlighted words are the answer.
Must-remember facts
| & in scanf | Gives the address to store input |
|---|---|
| Header file | Declarations of library functions |
| return 0 | Successful end |
| do-while | Exit-controlled, runs at least once |
| String | char array ending with '\0' |
| . vs -> | Variable vs pointer member access |
| free() | Releases malloc memory, prevents leaks |
Frequently asked questions
- What questions are asked in a C lab viva?
- Questions about your program (logic, headers, special inputs) and short theory questions on loops, arrays, functions, pointers and structures.
- How should I answer viva questions?
- In one or two clear sentences, with a small example if needed, calmly and honestly.
- What is the most asked C viva question?
- "Why do we use & in scanf?": because scanf needs the variable's address.
Remember
- Prepare the 3 W's for every program: What, Which concepts, What if.
- Short, clear, confident.
Next Topic
Next lesson: Important Questions for C Exams. You will see the theory questions that appear most often in university C exams, with answer outlines.