How the examiner runs a C programming viva
The C lab practical has two parts: writing and executing one or two programs from the semester list, and a viva of five to ten minutes with your record open. The internal examiner knows what was taught; the external examiner, appointed for the end-semester practical, reads your code cold and asks about whatever catches the eye. Viva marks are a separate line in the practical marks under most university schemes, and they usually decide whether you get full marks or just a pass.
- Question one is from your own program: 'why did you use a do-while here?' or 'what does this ampersand in scanf do?'
- Question two is a definition: pointer, array, structure, storage class.
- Question three is a what-if: 'what if I remove the break?', 'what if the string has no null character?', 'what if I free this twice?'
- Question four, if you are doing well, goes deeper: pointer arithmetic, padding, dangling pointers, static variables.
- Many examiners also ask you to predict the output of two or three lines. Trace it slowly and aloud.
The two-sentence rule
State the concept in one sentence, give one example from your own program in the second, then stop. The examiner will ask a follow-up if they want more, and the follow-up is where the extra marks are.
C basics: data types, variables and operators
What is C, and who developed it?
C is a general-purpose, procedural, compiled language developed by Dennis Ritchie at Bell Labs in the early 1970s to write the Unix operating system. It is called a middle-level language because it has high-level constructs but also gives direct access to memory through pointers.
If asked why it is still taught first: it shows how memory and the compiler actually work.
What are the features of C?
C is fast, portable across machines when written to the standard, and small, with few keywords and a rich standard library. It is structured, supports modular programming through functions, and allows low-level memory access with pointers and bitwise operators.
What is the structure of a C program?
A C program has preprocessor directives such as the include lines at the top, global declarations, the main function where execution starts, and other user-defined functions. Inside each function come local declarations followed by statements, and main returns an integer to the operating system.
Why does main return int, and what does return 0 mean?
main returns an integer exit status to the operating system or the program that started it. Returning 0 means the program finished successfully; a non-zero value signals an error. The standard defines EXIT_SUCCESS and EXIT_FAILURE in stdlib.h for the same purpose.
What are tokens in C?
Tokens are the smallest units the compiler recognises: keywords, identifiers, constants, string literals, operators and punctuators such as semicolons and braces. In int marks equals 90 semicolon, there are five tokens: int, marks, the equals operator, 90 and the semicolon.
How many keywords does C have?
The original ANSI C, called C89 or C90, has 32 keywords, such as int, float, if, while, return, struct and static. Later standards added more, for example inline and restrict in C99, and C23 made bool, true and false proper keywords.
Say 32 for ANSI C; mentioning that later standards added more shows depth.
What are the rules for naming an identifier?
An identifier can contain letters, digits and underscores, must start with a letter or an underscore, cannot be a keyword, and is case-sensitive, so Total and total are different. Spaces and special characters such as hyphens are not allowed.
What are the basic data types in C and their sizes?
The basic types are char, int, float and double, with modifiers short, long, signed and unsigned. char is always one byte. On a typical 64-bit GCC system int is 4 bytes, float 4 and double 8; the standard only fixes minimum ranges, so sizes depend on the compiler.
If you used Turbo C, say int was 2 bytes there; that is exactly the point the examiner is testing.
What is the difference between a declaration and a definition?
A declaration tells the compiler the name and type, for example extern int count, or a function prototype. A definition also allocates storage or provides the function body. A variable can be declared many times but defined only once in the program.
What are format specifiers? Name the common ones.
Format specifiers tell printf and scanf the type of the value: percent d for int, percent f for float, percent lf for double in scanf, percent c for char, percent s for a string, percent u for unsigned, percent ld for long and percent p for a pointer address.
Why do we put an ampersand before variables in scanf but not before a string?
scanf needs the address of the variable so it can store the input there, and the ampersand gives that address. A character array name already decays to the address of its first element, so for percent s we pass the array name directly.
What is the difference between i plus plus and plus plus i?
Both increase i by one. Pre-increment, plus plus i, increments first and then uses the new value; post-increment, i plus plus, uses the old value and then increments. So if i is 5, x equals i plus plus gives x 5, while x equals plus plus i gives x 6.
What is the difference between the assignment operator and the equality operator?
A single equals sign assigns a value to a variable; a double equals compares two values and gives 1 if equal and 0 if not. Writing if x equals 5 with a single equals assigns 5 and the condition is always true, a classic bug.
Some programmers write the constant on the left, 5 double-equals x, so the compiler catches the typo.
What is integer division? What does 7 divided by 2 give in C?
When both operands are integers, C performs integer division and discards the fractional part, so 7 divided by 2 gives 3, and 7 modulo 2 gives the remainder 1. To get 3.5 at least one operand must be a float or double, for example 7.0 divided by 2.
What is type casting? How is it different from implicit conversion?
Implicit conversion is done automatically by the compiler, for example an int promoted to double in a mixed expression. Type casting is explicit: I write the type in brackets before the value, as in (float) sum divided by n, to get a decimal average from integer sum and count.
What are bitwise operators?
They work on individual bits: AND, OR, XOR, NOT, left shift and right shift. Left shifting an unsigned value by one multiplies it by two, and right shifting divides by two. I used AND with 1 to check whether a number is odd in the even-odd program.
What is the ternary operator?
It is the conditional operator with three operands: condition, question mark, value if true, colon, value if false. For example, max equals a greater-than b question mark a colon b. It is a compact replacement for a simple if-else that returns a value.
What is the sizeof operator?
sizeof is a compile-time operator, not a function, that gives the size in bytes of a type or variable. sizeof of an int array of ten elements gives 40 on a system with 4-byte int. Its result type is size_t, an unsigned integer type.
Control flow, functions and recursion
What is the difference between while and do-while loops?
A while loop checks the condition before the body, so it may run zero times. A do-while checks after the body, so it runs at least once. I used do-while for the menu-driven calculator because the menu must be shown at least once.
When do you use a for loop instead of a while loop?
I use for when the number of iterations is known, because initialisation, condition and update sit in one line, as in printing a table from 1 to 10. I use while when the loop depends on a condition, such as reading digits until the number becomes zero.
What is the difference between break and continue?
break exits the nearest loop or switch immediately. continue skips the rest of the current iteration and moves to the next one. In the prime-number program I used break to stop checking divisors as soon as one was found.
What happens if you omit break in a switch case?
Execution falls through into the next case and runs its statements as well, until a break or the end of the switch. Fall-through is sometimes deliberate, for grouping vowels under one action, but usually it is a bug.
The examiner may then ask what types a switch accepts: integer and character constant expressions, not float or strings.
What is an infinite loop? Give an example.
A loop whose condition never becomes false, so it never ends on its own, such as while(1) or for(;;). It is useful in servers or menu programs that exit with a break or return, but accidental ones usually come from forgetting to update the loop variable.
What is the goto statement, and why is it avoided?
goto jumps unconditionally to a labelled statement in the same function. It is avoided because it makes the flow hard to follow and debug, leading to so-called spaghetti code. The one accepted use is jumping to a common cleanup block when exiting nested loops on an error.
What is a function prototype, and why is it needed?
A prototype declares the function's return type, name and parameter types before it is used, for example int add(int, int) semicolon. It lets the compiler check calls for the right number and type of arguments when the definition comes after main or is in another file.
What is the difference between call by value and call by reference?
In call by value the function gets a copy, so changes inside do not affect the caller. C always passes by value, but I can get call-by-reference behaviour by passing a pointer, as in my swap program where swap took the addresses of a and b.
What are actual and formal parameters?
Actual parameters, or arguments, are the values passed in the function call, like add(x, y). Formal parameters are the variables in the function definition that receive those values, like int a and int b. Formal parameters are local to the function.
What is recursion? What is a base case?
Recursion is a function calling itself to solve a smaller version of the same problem. The base case is the condition where it stops calling itself; in my factorial program, factorial of 0 or 1 returns 1. Without a base case the calls never end and the stack overflows.
Recursion or iteration: which is better?
Iteration usually uses less memory and is faster, because every recursive call adds a stack frame. Recursion gives shorter, clearer code for naturally recursive problems such as tree traversal, Tower of Hanoi or quicksort. For factorial or Fibonacci, a loop is the better choice in C.
What is the difference between library functions and user-defined functions?
Library functions such as printf, strlen and sqrt are already written and declared in standard headers; I only include the header and call them. User-defined functions are written by the programmer, like the isPrime function in my record, to break the program into reusable parts.
Arrays and strings
What is an array?
An array is a collection of elements of the same data type stored in contiguous memory locations and accessed by an index starting from 0. int marks[5] reserves space for five integers, from marks[0] to marks[4]. The size must be known when the array is created.
Does C check array bounds? What happens if you access marks[10] in a five-element array?
No, C does not check bounds. Accessing marks[10] reads or writes memory outside the array, which is undefined behaviour: it may print garbage, silently corrupt another variable or crash with a segmentation fault. The programmer must keep the index within range.
How is a two-dimensional array stored in memory?
In row-major order: all elements of row 0 first, then row 1, and so on, in one contiguous block. So in int a[3][4], a[1][0] comes right after a[0][3]. That is why the column size must be given when a 2D array is passed to a function.
How do you pass an array to a function?
By passing the array name, which decays to a pointer to its first element, along with the size as a separate argument, because the function cannot find the length by itself. Since only the address is passed, changes made inside the function affect the original array.
What is a string in C?
C has no separate string type. A string is a character array terminated by the null character, backslash zero. The literal hello takes six bytes: five characters plus the terminator. String functions like strlen and printf with percent s depend on that terminator to find the end.
What is the difference between strlen and sizeof for a string?
strlen counts characters up to but not including the null terminator, at run time. sizeof gives the total bytes of the array at compile time. For char name[20] holding Ravi, strlen gives 4 and sizeof gives 20.
Why is gets unsafe, and what do you use instead?
gets does not know the size of the buffer, so a long input overflows the array and overwrites nearby memory. It was removed from the language in C11. I use fgets with the buffer size and stdin, and then remove the newline it keeps.
What does strcmp return?
strcmp compares two strings character by character and returns 0 if they are equal, a negative value if the first is smaller in dictionary order, and a positive value if it is larger. Comparing strings with double equals only compares addresses, which is why we need strcmp.
Name the common string library functions.
From string.h: strlen for length, strcpy and strncpy to copy, strcat and strncat to join, strcmp to compare, strchr and strstr to search, and memset or memcpy for raw memory. In my record I also wrote my own string reverse and palindrome check without library functions.
What is the time complexity of bubble sort and binary search?
Bubble sort is order n squared in the worst and average case, and order n in the best case if I stop when a pass makes no swaps. Binary search is order log n, but only works on a sorted array, while linear search is order n on any array.
Pointers: the heart of every C viva
What is a pointer?
A pointer is a variable that stores the memory address of another variable. I declare it with an asterisk, as int star p, store an address with p equals ampersand x, and read the value at that address with star p, which is called dereferencing.
Say the address-of and dereference operators by name; examiners listen for both words.
What is the size of a pointer?
The size depends on the architecture, not on the type it points to. On a 64-bit system a pointer is usually 8 bytes, whether it points to a char or a double; on a 32-bit system it is usually 4 bytes.
What is pointer arithmetic?
Adding an integer to a pointer moves it by that many elements, not bytes. If p is an int pointer at address 1000 and int is 4 bytes, p plus 1 is 1004. Subtracting two pointers into the same array gives the number of elements between them.
What is the relationship between arrays and pointers?
In most expressions an array name decays to a pointer to its first element, so arr[i] and star of arr plus i are the same. But an array is not a pointer: the array name cannot be reassigned, and sizeof an array gives the whole array size.
What is a null pointer?
A null pointer points to no valid object; it is set with NULL and checked before use. malloc returns NULL when it cannot allocate memory, so I always check the result. Dereferencing a null pointer is undefined behaviour and usually crashes the program.
What is a dangling pointer?
A pointer that still holds the address of memory that has been freed or has gone out of scope, such as a pointer to a local variable returned from a function. Using it is undefined behaviour. After calling free, I set the pointer to NULL.
What is a wild pointer?
A pointer that has been declared but not initialised, so it holds a garbage address. Writing through it can corrupt any memory. The fix is to initialise every pointer, either to a valid address or to NULL, at the point of declaration.
What is a void pointer?
A void pointer is a generic pointer that can hold the address of any data type. It cannot be dereferenced directly; I must cast it to the right type first. malloc returns a void pointer, which is converted to the needed pointer type.
What is a pointer to a pointer?
A pointer that stores the address of another pointer, declared with two asterisks, as int star star pp. It is used to change a pointer inside a function, such as updating the head of a linked list, and for dynamic two-dimensional arrays.
What is the difference between a pointer to a constant and a constant pointer?
With a pointer to a constant, const int star p, I cannot change the value through p, but p can point elsewhere. With a constant pointer, int star const p, the address is fixed but the value can be changed. Reading the declaration right to left helps.
What is a function pointer?
A pointer that stores the address of a function, so the function can be called through it or passed as an argument. qsort uses one: I pass a comparison function that tells it how to order the elements. Menu programs sometimes use an array of function pointers.
How do you swap two numbers using pointers?
I write swap(int star a, int star b), store star a in a temporary variable, assign star b to star a, then the temporary to star b, and call swap with ampersand x and ampersand y. Because the function works on the addresses, the original variables change.
What is the arrow operator used for?
The arrow operator, a hyphen followed by a greater-than sign, accesses a structure member through a pointer. p arrow marks is the same as star p in brackets dot marks. I used it throughout the linked list program, as in temp arrow next.
How is a linked list node defined in C?
With a self-referential structure: struct node containing an int data field and a pointer to struct node called next. Each node is created with malloc, and the last node's next is NULL. Unlike an array, the list grows at run time and insertion needs no shifting.
Structures, unions, dynamic memory and files
What is a structure?
A structure is a user-defined type that groups variables of different types under one name. In my student record program, struct student held a roll number as int, a name as a char array and marks as float, and I made an array of these structures.
What is the difference between a structure and a union?
In a structure every member has its own memory, so the size is at least the sum of the members. In a union all members share the same memory, so the size equals the largest member, rounded up for alignment, and only one member holds a valid value at a time.
What is structure padding?
The compiler inserts unused bytes between members so each member sits at an address suited to its alignment. A struct with a char and an int is usually 8 bytes, not 5. Ordering members from largest to smallest often reduces the padding.
What is typedef?
typedef creates an alias for an existing type. typedef struct node Node lets me write Node instead of struct node everywhere. It does not create a new type; it only gives a shorter, clearer name, which helps with structures and function pointers.
What is an enum?
An enumeration is a user-defined type with named integer constants, such as enum day with MON, TUE and WED. By default the first name is 0 and each next one is one more, unless I assign values. It makes code more readable than plain numbers.
What is the difference between static and dynamic memory allocation?
Static or automatic allocation is fixed at compile time, like int a[100], and memory is managed automatically. Dynamic allocation happens at run time from the heap with malloc, calloc or realloc, and must be released with free. I used dynamic allocation when the user entered the array size.
What is the difference between malloc and calloc?
malloc takes one argument, the total bytes, and leaves the memory uninitialised with garbage values. calloc takes two arguments, the number of elements and the size of each, and initialises all bytes to zero. Both return a void pointer, or NULL on failure.
What does realloc do?
realloc changes the size of a previously allocated block. It may extend the block in place or allocate a new one, copy the old contents and free the old block. I store its result in a temporary pointer first, because if it fails and returns NULL the original block is still valid.
What is a memory leak?
A memory leak happens when dynamically allocated memory is no longer reachable but was never freed, for example when the only pointer to it is overwritten. In a long-running program leaks keep growing until memory runs out. Every malloc should have exactly one matching free.
What is the difference between stack and heap memory?
The stack holds local variables and function call frames; it is fast and freed automatically when the function returns, but it is limited in size. The heap holds dynamically allocated memory; it is larger and lasts until free is called, but managing it is the programmer's job.
How do you open and close a file in C? What are the modes?
I declare a FILE pointer and call fopen with the file name and mode: r to read, w to write and truncate, a to append, and r plus, w plus or a plus to read and write. I check for NULL, then call fclose when done to flush and release the file.
What is EOF?
EOF is a macro, usually defined as minus 1, returned by functions like fgetc and fscanf when the end of the file is reached or an error occurs. That is why the result of fgetc is stored in an int, not a char, so EOF can be told apart from a valid character.
What is the difference between text and binary files?
A text file stores data as readable characters, and newline handling may be translated by the system. A binary file stores the exact bytes in memory format, read and written with fread and fwrite. Binary files are compact and faster for structures but not human-readable.
Storage classes, preprocessor and compilation
What are the storage classes in C?
auto, register, static and extern. A storage class decides a variable's scope, lifetime, default initial value and where it is stored. auto is the default for local variables; extern declares a variable defined in another file; register requests fast access, though modern compilers decide this themselves.
What is a static variable?
A static local variable is initialised only once and keeps its value between function calls, because it lives for the whole program, not on the stack. Its default value is zero. I used one to count how many times a function was called.
What does static mean for a global variable or function?
It gives internal linkage: the variable or function is visible only inside the source file where it is defined, not in other files of the program. This hides helper functions and avoids name clashes when several files are linked together.
What is the difference between scope and lifetime?
Scope is the part of the program where a name can be used, such as a block, a function or the whole file. Lifetime is how long the storage exists while the program runs. A static local variable has block scope but lasts for the entire program.
What is the preprocessor?
The preprocessor runs before compilation and handles lines starting with the hash sign: it pastes in header files for include, replaces macros defined with define, and keeps or removes code with conditional directives such as ifdef. The compiler then sees the expanded source.
What is the difference between a macro and a function?
A macro is replaced by text before compilation, so there is no call overhead and no type checking. A function is compiled once and called at run time with type-checked arguments. Macros can misbehave: SQUARE of x plus 1 expands wrongly unless every argument is wrapped in brackets.
What is a header guard?
A header guard uses ifndef, define and endif around a header file's contents so that if the header is included twice in the same file, its declarations are processed only once. It prevents redefinition errors in programs with several headers.
What are the stages from a C source file to a running program?
Preprocessing expands headers and macros; compilation turns C into assembly; assembly turns that into an object file; and linking joins object files with library code such as printf into an executable. The loader then places it in memory and starts main. gcc does all four by default.
What are command-line arguments?
Values passed to the program when it is run, received in main as int argc, the count, and char star argv[], the array of strings. argv[0] is the program name. In my file-copy program I passed the source and destination file names this way.
What is undefined behaviour? Give an example.
Behaviour the C standard places no requirements on, so the program may give any result or crash, and different compilers may differ. Examples are i equals i plus plus, reading an uninitialised variable, accessing an array out of bounds and dereferencing a freed pointer.
What is the volatile keyword?
volatile tells the compiler that a variable can change outside the program's control, for example a hardware register or a variable changed by an interrupt handler. The compiler must then read it from memory every time instead of optimising it into a register.
What is a segmentation fault?
A run-time error raised when a program accesses memory it is not allowed to, such as dereferencing a NULL or wild pointer, writing to a string literal, or overflowing the stack with endless recursion. The operating system stops the program. I debug it with gdb or by printing values step by step.
Predict-the-output questions examiners like
Many examiners write three lines on a slip and ask what is printed. Trace each line aloud, saying the value of every variable. The table gives the patterns that come up most often in first-year labs.
Common mistakes in a C viva
- Saying 'C passes arrays by reference'. C passes the address of the first element by value; the effect looks like call by reference.
- Saying an array and a pointer are the same thing. They behave alike in expressions, but sizeof and assignment show the difference.
- Giving int as always 2 bytes or always 4 bytes. It depends on the compiler and platform; name the one you used.
- Calling sizeof a function. It is an operator evaluated at compile time.
- Forgetting the null terminator when counting the bytes in a string.
- Explaining a line you copied but cannot trace. If your record has a construct you do not understand, learn it or rewrite the program before the exam.
- Answering in five sentences when two would do. Long answers invite interruptions and expose mistakes.
If you do not know
Offer what the code does instead of the term: 'Sir, I am not sure of the name, but this line keeps the value between calls because the variable is static.' Honest and short scores better than a confident wrong answer on pointers.
What the ExamPilot AI examiner checks when you practise
In a CS/IT lab viva session on ExamPilot you name the C program you wrote, say the linked list or the student-record file program, and an AI examiner questions you by voice for about ten minutes: a line from your code, a definition, a what-if, a trace and one deeper question. It interrupts when you ramble and asks 'why' after every 'what', the way an external examiner does. You can answer in English or Hindi.
Start with the CS/IT lab viva mock. You get a transcript, the questions you fumbled and a tighter version of each answer to say again.
The night before the C lab exam
- 1
Re-read every program in your record
Go line by line and mark each line you cannot explain. Those are the examiner's first questions.
- 2
Say the ten basics without looking
Pointer, null and dangling pointer, array versus pointer, string and null terminator, call by value, structure versus union, malloc versus calloc, static, storage classes, compilation stages.
- 3
Do three traces aloud
A loop with post-increment, a recursive factorial for n equals 4, and a pointer swap. Say every variable's value as you go.
- 4
Run one mock viva
Ten minutes with the AI examiner on your own program shows which answers come out cleanly and which ones you only recognise when you read them.
Other labs follow the same pattern: operating system viva questions, Java viva questions, computer network viva questions, DBMS viva questions and Python viva questions. For the approach behind every lab viva, read the engineering viva questions hub.
Frequently asked questions
- What are the most important C programming viva questions?
- Pointers and their types, the difference between an array and a pointer, strings and the null terminator, call by value versus call by reference, structures versus unions, malloc versus calloc and free, storage classes and static, recursion and its base case, and the stages of compilation. After these, expect questions on the exact programs in your record, so revise those first.
- How long should an answer in a C viva be?
- Two or three sentences, about twenty to thirty seconds. Give the definition, then one example from your own program, then stop. If the examiner wants more, they will ask a follow-up, and handling that follow-up well is what earns full viva marks. A long answer usually gets cut off, and it often contains the one mistake the examiner then chases.
- Do examiners ask output-prediction questions in a C viva?
- Often, especially external examiners. They write two or three lines with an increment, an integer division or a single equals sign in an if condition and ask what is printed. Trace each line aloud and name the rule you are applying, such as integer division dropping the fraction. A slow, correct trace scores better than a quick guess.
- Are C viva questions also asked in placement interviews?
- Yes. Service-company technical rounds for freshers commonly ask pointers, storage classes, structures versus unions and output-prediction questions, because C is in every first-year syllabus. The answers on this page work there too, but expect a small coding task as well. Pair this guide with the interview questions and answers for freshers guide.
- What if the examiner asks something we did not do in the lab?
- Say so in one sentence and give what you understand: 'We did not use function pointers in the lab, but I understand they store a function's address so it can be passed to qsort.' A calm partial answer shows understanding. Never claim to have used something your record does not contain, because the next question will be about it.
- How can I practise C viva questions out loud?
- Reading answers silently does not prepare you for being questioned. On ExamPilot you can run a CS/IT lab viva by voice: name your program, and the AI examiner asks about it, makes you trace a loop, tests a what-if on pointers and gives you a transcript with the answers to tighten. It is free to start, and you can answer in English or Hindi.
Practise out loud
Practise your C lab viva out loud
The AI examiner asks about the C program you wrote, makes you trace a loop, tests a what-if on pointers or memory, and scores correctness, brevity and clarity.
Engineering Viva · CS / IT lab viva
