back to course
Lesson 39 / 39100%· free preview
Exam Prep2/2
Viva Questions
Master C viva questions with clear answers, practical code, and tips to ace your interviews.
Common Viva Questions and Answers
Here you'll find a curated list of popular C language viva questions, with succinct answers, explanation tables, and short practical code snippets.
1. What is the difference between int main() and void main()?
- int main(): Standard and recommended; returns a value to the OS.
- void main(): Not standard; used in older compilers like Turbo C.
- Always use: int main()
2. What happens if we don’t write return 0; in main()?
- In modern C, the compiler automatically adds
return 0, so the program works as expected.
3. Why is C called a middle-level language?
- Supports low-level features like memory access via pointers.
- Supports high-level features like functions and loops.
4. What is the output?
c#include <stdio.h> int main() { printf("%d", printf("Hello")); return 0; }
Output
textHello5
printf("Hello")prints 'Hello' and returns 5 (the string's length), so the outerprintfprints 5.
5. What is the output?
c#include <stdio.h> int main() { int a = 5; printf("%d %d %d", a, a++, ++a); return 0; }
Output
textUndefined behavior
- The variable
ais modified more than once between sequence points. Output depends on the compiler and is not predictable.
6. What is a dangling pointer?
- A pointer pointing to memory that has been freed or deleted.
7. What is the difference between declaration and definition?
| Declaration | Definition |
|---|---|
| Declares variable | Allocates memory |
| Example: int a; | Example: int a=10; |
8. What is the output?
c#include <stdio.h> int main() { int x = 10; if (x = 5) printf("Yes"); else printf("No"); return 0; }
Output
textYes
x = 5is assignment, not comparison (==). Assignment returns 5 (true), so 'Yes' is printed.
Common Mistake: Use == for comparison.
Correct Version:
c#include <stdio.h> int main() { int x = 10; if (x == 5) printf("Yes"); else printf("No"); return 0; }
- Tip: Always use
==inifwhen comparing values.
9. What is the difference between ++i and i++?
| ++i | i++ |
|---|---|
| Pre-increment | Post-increment |
| First increase, then print | First print, then increase |
10. What is the size of int?
- Depends on the system/compiler. Usually 4 bytes.
11. What is the output?
c#include <stdio.h> int main() { char ch = 'A'; printf("%d", ch); return 0; }
Output
text65
- ASCII value of 'A'.
12. Can we access an array out of bounds?
- Yes, but it is dangerous: may give garbage values or cause a crash.
13. What is the difference between break and continue?
| Statement | Meaning | Example Code | Output |
|---|---|---|---|
| break | Exit loop completely | if(i == 3) break; | 1 2 |
| continue | Skip current iteration | if(i == 3) continue; | 1 2 4 5 |
14. What is the output?
c#include <stdio.h> int main() { int a = 0; if (a) printf("True"); else printf("False"); return 0; }
Output
textFalse
- In C, 0 is equivalent to false.
15. What is NULL?
- A pointer that points to nothing (address 0).
16. What is the difference between malloc() and free()?
malloc(): Allocates memory.free(): Releases allocated memory.
17. What is the output?
c#include <stdio.h> int main() { printf("%d", sizeof('A')); return 0; }
Output
text4
- In C, character literals are treated as
int.
18. What is call by value and call by reference?
c#include <stdio.h> // Call by Value void swapValue(int a, int b) { int temp = a; a = b; b = temp; } // Call by Reference void swapRef(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int main() { int x = 5, y = 10; swapValue(x, y); printf("After Call by Value: x=%d y=%d\n", x, y); // x=5 y=10 swapRef(&x, &y); printf("After Call by Reference: x=%d y=%d", x, y); // x=10 y=5 return 0; }
Memory trick: Value - copy (no change). Reference - address (change happens).
| Feature | Call by Value | Call by Reference |
|---|---|---|
| What is passed | Copy passed | Address passed |
| Effect on original | No change | Changes original |
| Example Code | swap(x, y); | swap(&x, &y); |
| Function Logic | void swap(int a, int b) | void swap(int *a, int *b) |
| Output | x=5, y=10 | x=10, y=5 |
19. What is stack and heap memory?
- Stack: Automatic memory for local variables.
- Heap: Dynamic memory allocated using
malloc()andfree().
20. What is segmentation fault?
- Accessing invalid memory causes a program crash (segmentation fault).
c#include <stdio.h> int main() { int *ptr = NULL; printf("%d", *ptr); // Invalid memory access return 0; }
21. What is the output?
c#include <stdio.h> int main() { int a = 5; printf("%d", a+++a); return 0; }
Output
text11
- Interpreted as: (a++) + a → 5 + 6 = 11
22. Can we compile a program without main()?
- No; for execution,
main()is required.
23. What is volatile keyword?
- Informs compiler not to optimize variable as its value may change unexpectedly.
24. What is the output?
c#include <stdio.h> int main() { int a = 3; printf("%d", a << 1); return 0; }
Output
text6
- Left shift operator multiplies the value by 2.
Key Takeaways
- Use
int main()for standard C programs. - Be careful with assignments and comparisons inside conditions.
- Always release dynamically allocated memory with
free(). - Misusing array indices or uninitialized pointers may lead to crashes.
- Understanding call by value vs. reference is vital for debugging code effects.
Interview Questions
Practice Questions
- Write a C function to show the difference between call by value and call by reference.
- Predict the output: int x = 0; if (x) printf("One"); else printf("Zero");
- Explain what happens when you use
breakandcontinuein a loop. - What is the output of printf("%d", sizeof('B'))?
- Show a C code example leading to a segmentation fault.
Pro Tips
- Always use
int main()and include a return statement. - Use
==for comparisons and=for assignments. - Avoid modifying variables more than once in a single expression.
- Never dereference NULL or uninitialized pointers.
- Release all dynamically allocated memory using
free(). - Understand how pre- and post-increment operators work.
AI-powered recap
Quick recap quiz?
We'll generate 5 MCQs from this lesson and check your understanding instantly. Takes ~30 seconds.
# program
Program
C
#include <stdio.h>
// Call by Value
void swapValue(int a, int b) {
int temp = a;
a = b;
b = temp;
}
// Call by Reference
void swapRef(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main(void) {
int x = 5, y = 10;
swapValue(x, y);
printf("After Call by Value: x=%d y=%d\n", x, y); // x=5 y=10
swapRef(&x, &y);
printf("After Call by Reference: x=%d y=%d\n", x, y); // x=10 y=5
return 0;
}Ready to move on?
// example library
Want more hands-on snippets in C?
Browse 360 runnable examples · across 36 chapters · short, copy-paste-friendly · grouped by topic
// suggested companion course
Data Structures with C
Comfortable with C? Level up — arrays · linked lists · stacks · queues · trees · graphs · sorting algorithms.
// did you know?
One surprising fact before your next lesson
Bite-sized programming facts that make your next coffee-break explanation land.
// feedback.matters()
Did this lesson help you?
