**Viva Questions and Answers on C Programming Topics**
MODULE 1
### 1. Introduction to C
**Q1. What is the C programming language?**
A: C is a general-purpose, structured programming language that is widely used for system
and application software development.
**Q2. Who developed C and in which year?**
A: C was developed by Dennis Ritchie at Bell Labs in 1972.
**Q3. What are the main features of C?**
A: Simplicity, portability, rich library, low-level memory access, modularity, and efficiency.
**Q4. Why is C called a middle-level language?**
A: Because it combines features of both high-level and low-level languages.
**Q5. What is the structure of a C program?**
A: A typical C program includes headers, main() function, declarations, and statements.
---
### 2. The C Character Set, Identifiers, and Keywords
**Q6. What are the different types of characters used in C?**
A: Letters, digits, special characters, and white spaces.
**Q7. What is the ASCII value of characters in C?**
A: It's a numeric representation of characters (e.g., A = 65).
**Q8. What are identifiers in C?**
A: Names used to identify variables, functions, arrays, etc.
**Q9. What are the rules for naming identifiers?**
A: Must start with a letter or underscore, followed by letters or digits. No spaces or special
characters.
**Q10. What are keywords in C?**
A: Reserved words with special meaning, e.g., int, return, if.
**Q11. How many keywords are there in C?**
A: There are 32 keywords in standard C.
**Q12. Can keywords be used as variable names?**
A: No, they are reserved.
---
### 3. Data Types, Constants, Variables
**Q13. What are the basic data types in C?**
A: int, float, char, double.
**Q14. What is the size of int, float, char, and double?**
A: Typically, int = 4 bytes, float = 4 bytes, char = 1 byte, double = 8 bytes (platform
dependent).
**Q15. What are derived data types?**
A: Arrays, functions, pointers.
**Q16. What is the difference between float and double?**
A: double has more precision and occupies more memory than float.
**Q17. What is a constant?**
A: A value that cannot be changed during program execution.
**Q18. How can you define a constant in C?**
A: Using `#define` or `const` keyword.
**Q19. What is the difference between #define and const?**
A: `#define` is handled by the preprocessor, while `const` is managed by the compiler.
**Q20. What is a variable?**
A: A named memory location used to store data.
**Q21. How do you declare and initialize variables in C?**
A: Example: `int a = 10;`
**Q22. What is the scope and lifetime of a variable?**
A: Scope defines where the variable can be accessed. Lifetime is how long the variable exists
in memory.
---
### 4. Declarations, Expressions, and Statements
**Q23. What is the purpose of a declaration in C?**
A: To specify the type and name of a variable before using it.
**Q24. Can a variable be declared without initialization?**
A: Yes.
**Q25. What is an expression in C?**
A: A combination of variables, constants, and operators that yields a result.
**Q26. What is the difference between expression and statement?**
A: Expression evaluates to a value; statement performs an action.
**Q27. What is a statement in C?**
A: A complete instruction to the compiler.
**Q28. What are different types of statements in C?**
A: Declaration, assignment, control statements, etc.
---
### 5. Data Input and Output
**Q29. What functions are used for single character input and output?**
A: `getchar()` and `putchar()`.
**Q30. What is the purpose of getchar() and putchar()?**
A: To read and write a single character.
**Q31. What is scanf() used for?**
A: For formatted input.
**Q32. How does printf() work?**
A: It is used for formatted output.
**Q33. What are format specifiers in scanf and printf()?**
A: Symbols like %d, %f, %c, %s used to format input/output.
**Q34. How do you take multiple inputs using scanf()?**
A: Example: `scanf("%d %f", &a, &b);`
---
### 6. Operators and Expressions
**Q35. What are arithmetic operators in C?**
A: +, -, \*, /, %
**Q36. What is the result of integer division?**
A: Only the integer part is returned.
**Q37. What is a unary operator?**
A: An operator that operates on a single operand (e.g., ++, --).
**Q38. What is the difference between ++i and i++?**
A: `++i` increments before use; `i++` increments after use.
**Q39. What are relational operators?**
A: <, >, <=, >=, ==, !=
**Q40. What are logical operators?**
A: &&, ||, !
**Q41. What is the difference between == and =?**
A: `==` checks equality; `=` is an assignment.
**Q42. What is an assignment operator?**
A: `=` assigns a value to a variable.
**Q43. What are compound assignment operators?**
A: +=, -=, \*=, /=, etc.
**Q44. What is the conditional operator in C?**
A: It’s a ternary operator: `condition ? expr1 : expr2`
**Q45. What is the syntax of the ternary operator?**
A: `condition ? true_expression : false_expression`
**Q46. What is type conversion?**
A: Converting one data type to another.
**Q47. What is the difference between implicit and explicit type conversion?**
A: Implicit is automatic; explicit uses casting.
**Q48. What is type casting?**
A: Manually converting one data type to another, e.g., `(float)a`
**Q49. What is typedef in C?**
A: Used to create a new name for an existing data type.
**Q50. Give an example of using typedef.**
A: `typedef int Marks;` Now `Marks` can be used instead of `int`.
---
### 7. Introduction to Preprocessor Directives
**Q51. What are preprocessor directives?**
A: Commands that are processed before compilation, starting with `#`.
**Q52. What is the purpose of #include?**
A: To include standard or user-defined header files.
**Q53. What does #define do?**
A: Defines macros or constants.
**Q54. What is the difference between #define and a constant variable?**
A: `#define` is replaced before compilation; `const` is checked at compile time.
**Q55. What is a macro in C?**
A: A fragment of code which has been given a name using `#define`.
**Q56. Can we use conditions inside macros? (e.g., #ifdef, #ifndef)**
A: Yes, used for conditional compilation.
**Q57. What happens during preprocessing?**
A: Header files are included, macros are expanded, conditional compilations are handled.
8. Control Statements
Q58. What is an if-else statement? A: It is a conditional branching statement used to
execute code blocks based on a condition.
Q59. What is the syntax of an if-else statement? A: if(condition) { // code } else { // code }
Q60. What is a switch statement? A: A multi-way branch statement based on the value of
an expression.
Q61. When do we use switch instead of if? A: When we have multiple values to compare
with a single variable.
Q62. What is the purpose of break statement? A: To exit a loop or switch block
prematurely.
Q63. What is the purpose of continue statement? A: To skip the current iteration and move
to the next loop cycle.
Q64. What are the different looping structures in C? A: while, do-while, for.
Q65. What is the difference between while and do-while loop? A: while checks the
condition first; do-while executes the body first.
Q66. What is the syntax of a for loop? A: for(initialization; condition; increment) { // code
}
9. Functions and Storage Classes
Q67. How do you define a function in C? A: With a return type, name, parameters, and
body: int add(int a, int b) { return a + b; }
Q68. How do you access a function? A: By calling it using its name and passing required
arguments.
Q69. What is a function prototype? A: A declaration of a function before its definition: int
add(int, int);
Q70. How do you pass arguments to a function? A: By value (default) or by pointer.
Q71. What are return values and their types? A: The value returned by a function can be
int, float, char, etc.
Q72. What are categories of functions? A: Functions with/without arguments and
with/without return value.
Q73. What is recursion? A: A function calling itself to solve smaller instances of the
problem.
Q74. What are storage classes in C? A: Keywords that define scope, visibility, and lifetime
of variables.
Q75. What is an automatic variable? A: Declared inside a function, has local scope, and is
created/destroyed on function call.
Q76. What is an external variable? A: Declared outside all functions, accessible globally.
Q77. What is a static variable? A: Retains value between function calls.
Q78. What is a register variable? A: Suggested to be stored in a CPU register for faster
access.
10. Arrays and Strings
Q79. What is an array in C? A: An array is a collection of elements of the same type stored
in contiguous memory locations.
Q80. How do you define an array? A: Example: int arr[5];
Q81. How are arrays processed in C? A: Through loops using indexing to access each
element.
Q82. What is a multidimensional array? A: An array with more than one index, like int
matrix[3][3];
Q83. How do you pass a 1-D array to a function? A: By passing the array name: void
display(int arr[])
Q84. How do you pass a 2-D array to a function? A: By specifying column size: void
display(int arr[][3])
Q85. What is a string in C? A: A sequence of characters terminated by a null character \0.
Q86. How is a string stored in C? A: As a 1-D array of characters ending with \0.
Q87. What is the use of puts() and gets()? A: gets() reads a string including spaces; puts()
prints a string.
Q88. What is an array of strings? A: A 2-D array where each row is a string: char
names[3][10];
Q89. How do you handle strings without using string functions? A: By using loops and
character arrays to process each character manually.
11. Pointers
Q95. What is a pointer in C?
A: A pointer is a variable that stores the memory address of another variable.
Q96. How do you declare a pointer in C?
A: data_type *pointer_name; Example: int *ptr;
Q97. How do you assign a value to a pointer?
A: By using the address-of operator &. Example: ptr = &a;
Q98. How do you access the value pointed by a pointer?
A: By using the dereference operator *. Example: *ptr = 10;
Q99. What are the operations that can be performed on pointers?
A: Dereferencing, pointer arithmetic (addition, subtraction), comparison, and assignment.
Q100. How do you pass pointers to a function?
A: By passing the pointer itself, allowing the function to modify the value of the variable at
the memory address. Example:
void modify(int *p) { *p = 5; }
int main() { int x = 10; modify(&x); }
Q101. What is the relation between pointers and one-dimensional arrays?
A: An array name is a pointer to the first element of the array. Example: arr[0] is the same as
*arr.
Q102. Can pointers be used with multidimensional arrays?
A: Yes, pointers can be used to point to the first element of each row in a multidimensional
array.
Q103. What is an array of pointers?
A: It is an array where each element is a pointer. Example: int *arr[5];
Q104. What is the relation between pointers and strings in C?
A: A string is a pointer to the first character of a character array. Example: char *str =
"Hello";
Q105. What is multiple indirection in pointers?
A: It refers to pointers pointing to other pointers, such as int **ptr or char **str. Example:
int a = 10;
int *p = &a;
int **q = &p;
Q106. What is dynamic memory allocation?
A: Dynamic memory allocation allows allocating memory during program execution using
functions like malloc(), calloc(), realloc(), and free().
Q107. What does malloc() do?
A: Allocates a block of memory of a specified size and returns a pointer to the first byte.
Example: int *arr = malloc(sizeof(int) * 5);
Q108. What does calloc() do?
A: Allocates memory for an array of specified elements and initializes them to zero.
Example: int *arr = calloc(5, sizeof(int));
Q109. What does realloc() do?
A: It resizes a previously allocated memory block. Example:
arr = realloc(arr, sizeof(int) * new_size);
Q110. What does free() do?
A: It releases the dynamically allocated memory. Example: free(arr);
12. Structures and Unions
Q111. What is a structure in C?
A: A structure is a user-defined data type that groups different types of variables under a
single name.
Q112. How do you define a structure in C?
A: struct structure_name {
data_type member1;
data_type member2;
};
Q113. How do you declare and initialize a structure variable?
A: struct person { char name[20]; int age; };
struct person p1 = {"John", 30};
Q114. How do you access members of a structure?
A: By using the dot operator. Example: p1.age or p1.name
Q115. What is a user-defined data type?
A: A data type defined by the user, such as struct or union.
Q116. Can structures contain pointers?
A: Yes, structures can contain pointers. Example:
struct person { char *name; int age; };
Q117. How do you pass a structure to a function?
A: Structures can be passed by value or by reference. Passing by reference is done using
pointers. Example:
void func(struct person *p) { printf("%s", p->name); }
Q118. What is a self-referential structure?
A: A structure that contains a pointer to itself. Example:
struct node {
int data;
struct node *next;
};
Q119. What is a union in C?
A: A union is a special data type that allows storing different data types in the same memory
location. All members of a union share the same memory location.
Q120. How do you define and use a union?
A:
union data {
int i;
float f;
char c;
};
union data d1;
d1.i = 10;
Q121. What is the difference between a structure and a union?
A: In a structure, each member has its own memory, while in a union, all members share the
same memory location.
Q122. What is a data file in C?
A: A data file is used to store data permanently on disk, which can be read or written during
program execution.
Q123. How do you open a file in C?
A: Using fopen() function:
FILE *fp = fopen("filename.txt", "mode");
Q124. What are different file opening modes?
A:
• "r" – Open for reading
• "w" – Open for writing (creates new or overwrites)
• "a" – Append to file
• "r+" – Read and write
• "w+" – Write and read (creates or overwrites)
• "a+" – Append and read
Q125. How do you close a file in C?
A: Using fclose() function. Example: fclose(fp);
Q126. How do you read from a file in C?
A: Using functions like fgetc(), fgets(), fscanf(), or fread().
Q127. How do you write to a file in C?
A: Using functions like fputc(), fputs(), fprintf(), or fwrite().
Q128. What are some common file handling functions?
A:
fopen(), fclose()
fgetc(), fputc()
fgets(), fputs()
fprintf(), fscanf()
fread(), fwrite()
feof(), fseek(), ftell()
Q129. What are formatted input/output functions?
A: Functions like fprintf() and fscanf() are used to perform formatted I/O on files, similar to
printf() and scanf().
Q130. How to process a file line-by-line?
A:
char line[100];
while (fgets(line, sizeof(line), fp) != NULL) {
printf("%s", line);
Q131. What is the difference between text and binary files?
A:
• Text file: Human-readable characters. Uses fprintf(), fscanf().
• Binary file: Stores data in binary format. Uses fread(), fwrite().
Q132. How do you read/write binary files?
A: fwrite(&structure, sizeof(structure), 1, fp);
fread(&structure, sizeof(structure), 1, fp);
14. Additional Features of C
Enumeration
Q133. What is an enumeration in C?
A: enum defines a user-defined type consisting of named integer constants. Example:
enum color { RED, GREEN, BLUE };
Q134. Can you assign custom values in enum?
A: Yes.
enum color { RED = 1, GREEN = 3, BLUE = 5 };
Bitwise Operators
Q135. What are bitwise operators in C?
A: Operators that work on bits:
• & (AND)
• | (OR)
• ^ (XOR)
• ~ (NOT)
• << (Left shift)
• >> (Right shift)
Q136. Example of bitwise AND?
A:
int a = 5, b = 3; // 5 = 0101, 3 = 0011
int c = a & b; // c = 0001 => 1
Command Line Parameters
Q137. What are command-line arguments in C?
A: Parameters passed to the main() function via terminal. Syntax:
int main(int argc, char *argv[])
Q138. What is argc and argv[]?
argc: Argument count
argv[]: Argument vector (array of strings)
Macros
Q139. What is a macro in C?
A: A macro is a fragment of code defined by #define and replaced by the preprocessor.
Q140. Example of a macro definition:
A: #define PI 3.14
Q141. What are function-like macros?
A: Macros that take arguments:
#define SQUARE(x) ((x)*(x))
Q142. What are conditional macros?
A: Preprocessor directives that compile code based on conditions.
#ifdef DEBUG
printf("Debugging...\n");
#endif