Here are the top 25 viva questions with answers for C programming:
1. Basics of C
1. What is C programming?
C is a general-purpose, structured, and procedural programming language developed
by Dennis Ritchie in the 1970s. It is widely used for system programming and
developing operating systems.
2. What are the key features of C?
Simple and efficient
Structured programming language
Rich library of functions
Low-level access to memory
Portability across platforms
3. What is the structure of a C program?
A C program typically includes:
1. Preprocessor directives (e.g., #include)
2. Main function (int main())
3. Declarations and executable statements
4. Return statement (return 0;).
2. Data Types and Variables
4. What are the basic data types in C?
int: Integer type
float: Floating-point type
char: Character type
double: Double-precision floating-point type
5. What is the difference between a variable and a constant?
Variable: A memory location whose value can change during program execution.
Constant: A memory location whose value remains fixed throughout the program.
6. What is the size of int, float, and char in C?
int: Usually 4 bytes
float: 4 bytes
char: 1 byte
(The sizes may vary depending on the system and compiler.)
3. Operators and Control Statements
7. What are the different types of operators in C?
Arithmetic: +, -, *, /, %
Relational: ==, !=, <, >, <=, >=
Logical: &&, ||, !
Bitwise: &, |, ^, ~, <<, >>
Assignment: =, +=, -=, etc.
8. What is the difference between if and switch statements?
if statement: Used for complex conditions; allows ranges and logical expressions.
switch statement: Used for discrete values; evaluates a single expression against multiple
cases.
9. What is a loop? Name the types of loops in C.
A loop allows a set of instructions to be executed repeatedly.
Types:
1. for loop
2. while loop
3. do-while loop
4. Functions
10. What is a function in C?
A function is a block of code designed to perform a specific task. It can be reused and
reduces code redundancy.
11. What is the difference between call by value and call by reference?
Call by value: Passes a copy of the value; changes do not affect the original variable.
Call by reference: Passes the address of the variable; changes affect the original variable.
12. What is recursion?
Recursion is a technique where a function calls itself directly or indirectly to solve a
problem.
5. Arrays and Strings
13. What is an array?
An array is a collection of elements of the same data type, stored in contiguous
memory locations.
14. How is a string represented in C?
A string in C is an array of characters terminated by a null character (\0).
15. What is the difference between strlen() and sizeof()?
strlen(): Returns the length of the string (excluding \0).
sizeof(): Returns the size of the variable or data type in bytes.
6. Pointers
16. What is a pointer?
A pointer is a variable that stores the memory address of another variable.
17. What are the advantages of using pointers in C?
Efficient array and string manipulation
Dynamic memory allocation
Passing large structures to functions efficiently
Accessing hardware-level memory
18. What does the * and & operator do in C?
*: Dereference operator, retrieves the value from the memory address a pointer holds.
&: Address-of operator, retrieves the memory address of a variable.
7. Storage Classes
19. What are storage classes in C?
Storage classes define the scope, visibility, and lifetime of variables. Types:
1. auto: Default for local variables.
2. register: Stores variables in CPU registers.
3. static: Preserves the value of a variable between function calls.
4. extern: Declares a global variable defined elsewhere.
8. File Handling
20. How are files handled in C?
C uses FILE pointers and standard library functions like fopen(), fclose(),
fscanf(), fprintf(), fread(), and fwrite() for file handling.
21. What are the different file modes in C?
"r": Read mode
"w": Write mode
"a": Append mode
"r+": Read and write
"w+": Overwrite and read
"a+": Append and read
9. Memory Management
22. What is dynamic memory allocation in C?
Dynamic memory allocation allows programs to allocate memory at runtime using
functions like malloc(), calloc(), realloc(), and free().
23. What is the difference between malloc() and calloc()?
malloc(): Allocates a single block of memory, uninitialized.
calloc(): Allocates multiple blocks of memory, initialized to 0.
10. Miscellaneous
24. What is the purpose of the #include directive?
The #include directive includes the contents of a header file in the program. For
example, #include <stdio.h> includes standard input/output functions.
25. What is the difference between #define and const?
#define: Preprocessor directive for defining macros, replaced during preprocessing.
const: A keyword to declare a variable whose value cannot be modified during runtime.
Let me know if you need more questions or explanations for any specific topic!