C Language Viva Questions
C Language Viva Questions
Recursion
What is dynamic memory allocation (DMA)? DMA is a feature that allows users to allocate
memory locations during runtime.
File operations
How do you perform file operations in C? File operations are performed using standard library
functions like fopen, fclose, fread, fwrite, fgets, and fprintf.
How do compilers and interpreters compare and contrast? Compilers and interpreters both
execute program codes, but they do so differently.
Preprocessor
What is a preprocessor? Preprocessor directives in C start with a hash symbol (#) and are
processed before the program is compiled.
What are static variables and functions? Variables and functions declared using the keyword
Static are considered static.
Array rotation
What is array rotation? Array rotation is a task where you write a C function that takes an array
and its length as input and rotates the array in-place to the right by one position.
What are increment and decrement operators? Increment and decrement operators are used to
change the value of an operand (constant or variable) by 1.
What is C language?
C is a mid-level and procedural programming language. The Procedural
programming language is also known as the structured programming language
is a technique in which large programs are broken down into smaller modules,
and each module uses structured code. This technique minimizes error and
misinterpretation.
scanf(): The scanf() function is used to take input from the user.
○ C functions are used to avoid the rewriting the same code again and
again in our program.
○ C functions can be called any number of times from any place of our
program.
○ When a program is divided into functions, then any part of our program
can easily be tracked.
○ C functions provide the reusability concept, i.e., it breaks the big task
into smaller tasks so that it makes the C program more understandable.
What is recursion in C?
When a function calls itself, and this process is known as recursion. The function
that calls itself is known as a recursive function.
What is an array in C?
An Array is a group of similar types of elements. It has a contiguous memory
location. It makes the code optimized, easy to traverse and easy to sort. The size
and type of arrays cannot be changed after its declaration.
Syntax:
data_type array_name[size];
Multidimensional array: Multidimensional array is an array that contains
more than one array.
Syntax:
data_type array_name[size];
What is a pointer in C?
A pointer is a variable that refers to the address of a value. It makes the code
optimized and makes the performance fast. Whenever a variable is declared
inside a program, then the system allocates some memory to a variable. The
memory contains some address number. The variables that hold this address
number is known as the pointer variable.
For example:
Data_type *p;
Test it Now
The above syntax releases the memory from a pointer variable ptr.What is the
difference between malloc() and calloc()?
calloc() malloc()
What is a union?
○ The union is a user-defined data type that allows storing multiple types
of data in a single unit. However, it doesn't occupy the sum of the
memory of all members. It holds the memory of the largest member
only.
○ In union, we can access only one variable at a time as it allocates one
common space for all the members of a union.
But, if we use #define, we can compile and run a C program without using the
main() function.
What is a token?
The Token is an identifier. It can be constant, keyword, string literal, etc. A token is
the smallest individual unit in a program.
The getche() function reads a single character from the keyword, but data is
displayed on the output screen. Press Alt+f5 to see the entered character.
What is typecasting?
The typecasting is a process of converting one data type into another is known as
typecasting. If we want to store the floating type value to an int type, then we will
convert the data type into another data type explicitly.
4. What is a macro?
○ Macros can take arguments, allowing for more flexible and powerful
definitions.
8. How do you define a macro with arguments?
Conditional Compilation
● These directives allow you to create conditional blocks of code that are
compiled or ignored based on the specified conditions.
● What is a file in C programming?
○ Read modes:
■ r: Opens a file for reading.
■ rb: Opens a file for reading in binary mode.
○ Write modes:
■ w: Creates a new file for writing.
■ wb: Creates a new file for writing in binary mode.
■ a: Opens a file for appending.
■ ab: Opens a file for appending in binary mode.
○ Read-write modes:
■ r+: Opens a file for both reading and writing.
■ w+: Creates a new file for both reading and writing.
■ a+: Opens a file for both reading and appending.
● Explain the fopen() function.
File Positioning
1. What is a structure in C?
○ variable_name.member_name
Structure Arrays
○ array_name[index].member_name
Nested Structures
● outer_variable.inner_member.member2
Pointers to Structures
Here are some common C pointer viva questions, along with explanations:
Basic Concepts
1. What is a pointer in C?
○ data_type *pointer_name;
3. How do you assign the address of a variable to a pointer?
○ pointer_name = &variable_name;
4. How do you access the value stored at the address pointed to by a
pointer?
○ *pointer_name
Pointer Arithmetic
Pointer to Pointer
○ data_type **pointer_name;
10. How do you access the value pointed to by a pointer to a pointer?
● **pointer_name
○ A function can return only one value, but you can use pointers or
structures to return multiple values indirectly.
What is an algorithm?
Common Algorithms
Operators
1. What are the different types of operators in C?
○ Arithmetic operators: +, -, *, /, %
○ Relational operators: ==, !=, >, <, >=, <=
○ Logical operators: &&, ||, !
○ Bitwise operators: &, |, ^, ~, <<, >>
○ Assignment operators: =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
○
○ Increment and decrement operators: ++, --
○ Conditional operator: ?:
○
○ Comma operator: ,
2. Explain the difference between pre-increment and post-increment
operators.
Control Structures
Structure of a C Program
○ Preprocessor directives
○ Global declarations
○ Main function
○ User-defined functions
6. Explain the role of the main() function.
Data Types
○ int: Integer
○ float: Floating-point number
○ double: Double-precision floating-point number
○ char: Character
9. What are keywords in C?
● Variables: Memory locations that store data, which can change during
program execution.
● Constants: Fixed values that cannot be changed during program
execution.
●
Input/Output Functions
1. What are the basic input/output functions in C?
○ printf() and scanf() are the most commonly used functions for
formatted input and output.
2. Explain the printf() function.
Formatted Input/Output
File Input/Output