0% found this document useful (0 votes)
41 views22 pages

C Language Viva Questions

Uploaded by

ysamuhith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views22 pages

C Language Viva Questions

Uploaded by

ysamuhith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Here are some common C language viva questions for Lab External

Recursion

What is recursion in C? Recursion is when a function calls itself directly or indirectly.

Dynamic memory allocation

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.

Compilers and interpreters

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.

Static variables and functions

What are static variables and functions? Variables and functions declared using the keyword
Static are considered static.

Printf() and scanf() functions

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.

Increment and decrement operators

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.

Why is C known as a mother language?


C is known as a mother language because most of the compilers and JVMs are
written in C language. Most of the languages which are developed after C
language has borrowed heavily from it like C++, Python, Rust, javascript, etc. It
introduces new core concepts like arrays, functions, file handling which are used
in these languages.

Why is C called a mid-level programming language?


C is called a mid-level programming language because it binds the low level and
high -level programming language. We can use C language as a System
programming to develop the operating system as well as an Application
programming to generate menu driven customer driven billing system.

Who is the founder of C language?


Dennis Ritchie

When was C language developed?


C language was developed in 1972 at bell laboratories of AT&T.

What are the features of the C language?


The main features of C language are given below:

○ Simple: C is a simple language because it follows the structured


approach, i.e., a program is broken into parts
○ Portable: C is highly portable means that once the program is written
can be run on any machine with little or no modifications.
○ Mid Level: C is a mid-level programming language as it combines the
low- level language with the features of the high-level language.
○ Structured: C is a structured language as the C program is broken into
parts.
○ Fast Speed: C language is very fast as it uses a powerful set of data types
and operators.
○ Memory Management: C provides an inbuilt memory function that
saves the memory and improves the efficiency of our program.
○ Extensible: C is an extensible language as it can adopt new features in
the future.

What is the use of printf() and scanf() functions?


printf(): The printf() function is used to print the integer, character, float and
string values on to the screen.

Following are the format specifier:

○ %d: It is a format specifier used to print an integer value.


○ %s: It is a format specifier used to print a string.
○ %c: It is a format specifier used to display a character value.
○ %f: It is a format specifier used to display a floating point value.

scanf(): The scanf() function is used to take input from the user.

What is the difference between the local variable and global


variable in C?
Following are the differences between a local variable and global variable:

Basis for comparison Local variable Global variable

Declaration A variable which is A variable which is


declared inside declared outside
function or block is function or block is
known as a local known as a global
variable. variable.

Scope The scope of a variable The scope of a variable


is available within a is available throughout
function in which they the program.
are declared.

Access Variables can be Any statement in the


accessed only by those entire program can
statements inside a access variables.
function in which they
are declared.
Life Life of a variable is Life of a variable exists
created when the until the program is
function block is executing.
entered and destroyed
on its exit.

Storage Variables are stored in a The compiler decides


stack unless specified. the storage location of
a variable.

What is the use of the function in C?


Uses of C function are:

○ 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 the difference between call by value and call by


reference in C?
Following are the differences between a call by value and call by reference are:

Call by value Call by reference

Description When a copy of the When a copy of the


value is passed to the value is passed to the
function, then the function, then the
original value is not original value is
modified. modified.

Memory location Actual arguments and Actual arguments and


formal arguments are formal arguments are
created in separate created in the same
memory locations. memory location.

Safety In this case, actual In this case, actual


arguments remain safe arguments are not
as they cannot be reliable, as they are
modified. modified.

Arguments The copies of the actual The addresses of actual


arguments are passed arguments are passed
to the formal to their respective
arguments. formal arguments.

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.

Arrays are of two types:

○ One-dimensional array: One-dimensional array is an array that stores


the elements one after the another.

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;

What is the usage of the pointer in C?


○ Accessing array elements: Pointers are used in traversing through an
array of integers and strings. The string is an array of characters which is
terminated by a null character '\0'.
○ Dynamic memory allocation: Pointers are used in allocation and
deallocation of memory during the execution of a program.
○ Call by Reference: The pointers are used to pass a reference of a variable
to other function.
○ Data Structures like a tree, graph, linked list, etc.: The pointers are used
to construct different data structures like tree, graph, linked list, etc.

What is a NULL pointer in C?


A pointer that doesn't refer to any address of value but NULL is known as a NULL
pointer. When we assign a '0' value to a pointer of any type, then it becomes a
Null pointer.

What is a far pointer in C?


A pointer which can access all the 16 segments (whole residence memory) of
RAM is known as far pointer. A far pointer is a 32-bit pointer that obtains
information outside the memory in a given section.

What is dangling pointer in C?


○ If a pointer is pointing any memory location, but meanwhile another
pointer deletes the memory occupied by the first pointer while the first
pointer still points to that memory location, the first pointer will be
known as a dangling pointer. This problem is known as a dangling
pointer problem.
○ Dangling pointer arises when an object is deleted without modifying
the value of the pointer. The pointer points to the deallocated memory.

What is pointer to pointer in C?


In case of a pointer to pointer concept, one pointer refers to the address of
another pointer. The pointer to pointer is a chain of pointers. Generally, the
pointer contains the address of a variable. The pointer to pointer contains the
address of a first pointer.

What is static memory allocation?


○ In case of static memory allocation, memory is allocated at compile
time, and memory can't be increased while executing the program. It is
used in the array.
○ The lifetime of a variable in static memory is the lifetime of a program.

What is dynamic memory allocation?


○ In case of dynamic memory allocation, memory is allocated at runtime
and memory can be increased while executing the program. It is used
in the linked list.
○ The malloc() or calloc() function is required to allocate the memory at
the runtime.

What functions are used for dynamic memory allocation in C


language?
1. malloc()
​ The malloc() function is used to allocate the memory during the
execution of the program.
​ It does not initialize the memory but carries the garbage value.
​ It returns a null pointer if it could not be able to allocate the
requested space.
2. Syntax
​ ptr = (cast-type*) malloc(byte-size) // allocating the memory using malloc()
function.
3. Test it Nowcalloc()
​ The calloc() is same as malloc() function, but the difference only is
that it initializes the memory with zero value.
4. Syntax
​ ptr = (cast-type*)calloc(n, element-size);// allocating the memory using
calloc() function.
5. Test it Nowrealloc()
​ The realloc() function is used to reallocate the memory to the new
size.
​ If sufficient space is not available in the memory, then the new
block is allocated to accommodate the existing data.
6. Syntax
​ ptr = realloc(ptr, newsize); // updating the memory size using realloc()
function.
7. Test it Now
In the above syntax, ptr is allocated to a new size.free():The free() function
releases the memory allocated by either calloc() or malloc() function.
8. Syntax
​ free(ptr); // memory is released using free() function.

Test it Now
The above syntax releases the memory from a pointer variable ptr.What is the
difference between malloc() and calloc()?
calloc() malloc()

Description The malloc() function The calloc() function


allocates a single block allocates multiple
of requested memory. blocks of requested
memory.

Initialization It initializes the content It does not initialize the


of the memory to zero. content of memory, so
it carries the garbage
value.

Number of arguments It consists of two It consists of only one


arguments. argument.

Return value It returns a pointer It returns a pointer


pointing to the pointing to the
allocated memory. allocated memory.

What is the structure?


○ The structure is a user-defined data type that allows storing multiple
types of data in a single unit. It occupies the sum of the memory of all
members.
○ The structure members can be accessed only through structure
variables.
○ Structure variables accessing the same structure but the memory
allocated for each variable will be different.

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.

What is an auto keyword in C?


In C, every local variable of a function is known as an automatic (auto) variable.
Variables which are declared inside the function block are known as a local
variable. The local variables are also known as an auto variable. It is optional to
use an auto keyword before the data type of a variable. If no value is stored in the
local variable, then it consists of a garbage value.

Can we compile a program without main() function?


Yes, we can compile, but it can't be executed.

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.

What is command line argument?


The argument passed to the main() function while executing the program is
known as command line argument

What is the difference between getch() and getche()?


The getch() function reads a single character from the keyboard. It doesn't use
any buffer, so entered data will not be displayed on the output screen.

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.

What are the functions to open and close the file in C


language?
The fopen() function is used to open file whereas fclose() is used to close file.

Can we access the array using a pointer in C language?


Yes, by holding the base address of array into a pointer, we can access the array
using a pointer.

What is an infinite loop?


A loop running continuously for an indefinite number of times is called the
infinite loop.

What is a preprocessor directive?

○ A preprocessor directive is a command given to the C preprocessor


to process the source code before compilation. It starts with a #
symbol.
2. What are the main preprocessor directives in C?

○ #include: Includes header files or other source files.


○ #define: Defines macros or symbolic constants.
○ #undef: Undefines a previously defined macro.
○ #ifdef: Checks if a macro is defined.
○ #ifndef: Checks if a macro is not defined.
○ #if: Conditional compilation based on a constant expression.
○ #else: Alternative code block for #if or #ifdef.
○ #elif: Combines #else and #if for multiple conditions.
○ #endif: Marks the end of a conditional block.
3. What is the difference between #include <filename> and #include
"filename"?

○ #include <filename>: Searches for the header file in standard


system directories.
○ #include "filename": Searches for the header file in the current
directory first, then in standard system directories.

Macros and Macros with Arguments

4. What is a macro?

○ A macro is a symbolic name that is replaced by a text string before


compilation.
5. How do you define a macro?

○ #define MACRO_NAME replacement_text


6. What are the advantages and disadvantages of macros?
○ Advantages:
■ Faster execution due to in-line expansion.
■ Can be used to create reusable code snippets.
○ Disadvantages:
■ No type checking, leading to potential errors.
■ Can be difficult to debug due to macro expansion.
■ Excessive use can make code less readable.
7. What are macros with arguments?

○ Macros can take arguments, allowing for more flexible and powerful
definitions.
8. How do you define a macro with arguments?

○ #define MACRO_NAME(arg1, arg2, ...) replacement_text

Conditional Compilation

9. What is conditional compilation?

○ Conditional compilation allows you to include or exclude specific


code sections based on certain conditions.
10. How do you use #ifdef, #ifndef, #if, #else, and #endif for 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?

○A file is a named collection of data stored on a secondary storage


device.
○ It can be a text file or a binary file.
● What are the modes for opening a file in C?

○ 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.

○ fopen() is used to open a file.


Syntax: FILE *fp = fopen(filename, mode);

It returns a file pointer fp that can be used to perform operations

on the file.
● Explain the fclose() function.

○ fclose() is used to close a file.


○ Syntax: fclose(fp);
○ It releases the resources associated with the file.

File Input/Output Operations

5. Explain the fgetc() function.

○ fgetc() reads a single character from a file.


○ Syntax: char ch = fgetc(fp);
6. Explain the fputc() function.

○ fputc() writes a single character to a file.


○ Syntax: fputc(ch, fp);
7. Explain the fgets() function.

○ fgets() reads a line of text from a file.


○ Syntax: fgets(str, size, fp);
8. Explain the fputs() function.

○ fputs() writes a string to a file.


○ Syntax: fputs(str, fp);
9. Explain the fscanf() function.

○ fscanf() reads formatted input from a file.


○ Syntax: fscanf(fp, format_string, &var1, &var2, ...);
10. Explain the fprintf() function.

● fprintf() writes formatted output to a file.


● Syntax: fprintf(fp, format_string, var1, var2, ...);

File Positioning

11. Explain the fseek() function.


● fseek() sets the file position indicator to a specific location.
● Syntax: fseek(fp, offset, origin);
12. Explain the ftell() function.
● ftell() returns the current position of the file pointer.
● Syntax: long pos = ftell(fp);
13. Explain the rewind() function.
● rewind() sets the file position indicator to the beginning of the file.
● Syntax: rewind(fp);
Basic Concepts

1. What is a structure in C?

○ A structure is a user-defined data type that groups variables of


different data types under a single name.
2. How do you declare a structure?

○ struct struct_name { data_type member1; data_type


member2; // ... };
3. How do you define a structure variable?

○ struct struct_name variable_name;


4. How do you access members of a structure?

○ variable_name.member_name

Structure Arrays

5. What is a structure array?

○ A structure array is an array of structures, where each element of


the array is a structure.
6. How do you declare a structure array?

○ struct struct_name array_name[size];


7. How do you access members of a structure array?

○ array_name[index].member_name

Nested Structures

8. What is a nested structure?

○ A nested structure is a structure that contains another structure


as a member.
9. How do you declare a nested structure?

○ struct outer_struct { int member1; struct inner_struct {


int member2; char member3; } inner_member; };
10. How do you access members of a nested structure?

● outer_variable.inner_member.member2

Pointers to Structures

11. How do you declare a pointer to a structure?


● struct struct_name *ptr;
12. How do you access members of a structure using a pointer?
● ptr->member_name or (*ptr).member_name

Structure and Functions

13. How do you pass a structure to a function?


● Pass by value: A copy of the structure is passed to the function.
● Pass by reference: A pointer to the structure is passed to the function.

14. How do you return a structure from a function?


● The function must return a pointer to the structure.

C Language Pointers Viva Questions

Here are some common C pointer viva questions, along with explanations:

Basic Concepts

1. What is a pointer in C?

○ A pointer is a variable that stores the memory address of another


variable.
2. How do you declare a pointer variable?

○ 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

5. What is pointer arithmetic?

○ Pointer arithmetic involves adding or subtracting integers to


pointers.
6. How does pointer arithmetic work with different data types?

○ The increment or decrement of a pointer depends on the size of


the data type it points to.
7. What are the limitations of pointer arithmetic?

○ You cannot add or subtract pointers of different data types.


○ You cannot compare pointers unless they point to elements of
the same array.

Pointer to Pointer

8. What is a pointer to a pointer?

○ A pointer to a pointer is a pointer that stores the address of


another pointer.
9. How do you declare a pointer to a pointer?

○ data_type **pointer_name;
10. How do you access the value pointed to by a pointer to a pointer?

● **pointer_name

Pointers and Arrays

11. How are arrays and pointers related in C?


● The name of an array is a constant pointer to the first element of the
array.
12. How can you access array elements using pointer arithmetic?
● *(array_name + index)

Pointers and Functions

13. How can you pass pointers to functions?


● By passing the address of a variable to the function.
14. How can you return a pointer from a function?
● By returning the address of a variable or dynamically allocated
memory.

Dynamic Memory Allocation

15. What are malloc() and calloc() functions?


● malloc() allocates a block of memory of specified size.
● calloc() allocates multiple blocks of memory of specified size and
initializes them to zero.
16. What is free() function?
● free() deallocates a block of memory previously allocated by malloc()
or calloc().
1. What is a function in C?

○ A function is a block of code that performs a specific task. It is


defined once and can be called multiple times from different
parts of the program.
2. What are the two types of functions in C?

○ Library functions: Predefined functions provided by the C


standard library.
○ User-defined functions: Functions defined by the programmer.
3. What is the syntax of a function definition?

○ return_type function_name(parameter_list) { // function


body }
4. What is a function prototype?

○ A function prototype declares the function's return type, name,


and parameter list. It is usually placed before the main function.
5. What is the difference between a function declaration and a function
definition?

○ A declaration specifies the function's signature, while a definition


provides the actual implementation of the function.

Function Parameters and Arguments

6. What are function parameters and arguments?

○ Parameters: Variables declared in the function's definition to


receive values from the caller.
○ Arguments: Values passed to the function when it is called.
7. What are the two ways to pass arguments to a function?

○ Pass by value: A copy of the argument is passed to the function.


○ Pass by reference: The address of the argument is passed to the
function.

Function Return Value

8. What is the return statement?

○ The return statement is used to return a value from a function.


9. Can a function return multiple values?

○ A function can return only one value, but you can use pointers or
structures to return multiple values indirectly.

Function Scope and Storage Class

10. What is function scope?


● Variables declared within a function have function scope, meaning
they are only accessible within that function.
11. What are storage classes in C?
● Storage classes determine the lifetime and scope of variables. Common
storage classes are auto, static, extern, and register.
Recursive Functions

12. What is a recursive function?


● A recursive function is a function that calls itself directly or indirectly.
13. What are the base case and recursive case in a recursive function?
● Base case: The simplest case that can be solved directly.
● Recursive case: The case that reduces the problem to a smaller version
of itself.

What is an algorithm?

An algorithm is a step-by-step procedure or set of rules to solve a



problem.
● What are the basic steps involved in algorithm design?

Understanding the problem: Clearly define the problem and its



inputs and outputs.
○ Developing a solution: Devise a logical approach to solve the
problem.
○ Algorithm design: Write the algorithm in a clear and concise
manner.
○ Algorithm analysis: Analyze the algorithm's time and space
complexity.
○ Implementation: Implement the algorithm in a programming
language like C.
● Explain the concept of time and space complexity.

○ Time complexity: Measures the amount of time an algorithm


takes to execute as a function of the input size.
○ Space complexity: Measures the amount of memory an algorithm
uses as a function of the input size.

Common Algorithms

4. Explain the concept of sorting algorithms.

○ Sorting algorithms arrange elements of a list in a specific order


(ascending or descending).
○ Common sorting algorithms: Bubble sort, selection sort, insertion
sort, merge sort, quick sort, heap sort.
5. Explain the concept of searching algorithms.

○ Searching algorithms find a specific element within a list.


○ Common searching algorithms: Linear search, binary search.
6. Explain the concept of recursion.

○ Recursion is a technique where a function calls itself directly or


indirectly.
7. What is the difference between iterative and recursive algorithms?

○ Iterative algorithms: Use loops to solve a problem.


○ Recursive algorithms: Break down a problem into smaller
subproblems and solve them recursively.

Algorithm Design Techniques

8. Explain the divide and conquer strategy.

○ Divide and conquer breaks down a problem into smaller


subproblems, solves them independently, and combines the
solutions.
9. Explain the greedy algorithm strategy.

○ A greedy algorithm makes the optimal choice at each step,


hoping to find the global optimum.
10. Explain the dynamic programming strategy.

● Dynamic programming breaks down a problem into overlapping


subproblems and stores the solutions to avoid redundant calculations.

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.

○ Pre-increment: The value of the variable is incremented first, and


then the new value is used in the expression.
○ Post-increment: The current value of the variable is used in the
expression, and then the value is incremented.
3. What is the difference between logical AND (&&) and bitwise AND (&)?

○ Logical AND: Returns 1 if both operands are non-zero, otherwise


0.
○ Bitwise AND: Performs bit-by-bit AND operation on the operands.
4. What is the purpose of the bitwise complement operator (~)?

○ It inverts all the bits of an integer.


5. How does the conditional operator work?

○ expression1 ? expression2 : expression3


○ If expression1 is true, expression2 is evaluated, otherwise
expression3 is evaluated.

Control Structures

6. What are the different types of control structures in C?

○ Decision-making statements: if, if-else, else-if, switch


○ Looping statements: for, while, do-while
7. Explain the difference between while and do-while loops.

○ while: The condition is checked before the loop body is executed.


○ do-while: The loop body is executed at least once, and then the
condition is checked.
8. What is the break statement used for?

○ It is used to exit a loop or switch statement immediately.


9. What is the continue statement used for?

○ It is used to skip the current iteration of a loop and move to the


next iteration.
10. Explain the goto statement and why it is generally avoided.

● It is used to jump to a labeled statement within a function.


● It can make code less readable and harder to debug.

Basic Concepts
1. What is C programming language?

○ C is a general-purpose, procedural programming language


known for its efficiency and flexibility.
2. What are the key features of C?

○ Low-level memory access


○ Structured programming
○ Rich set of operators
○ Function-oriented programming
○ Portability
○ Efficiency
3. What is a compiler?

○ A compiler is a software program that translates source code


written in a programming language (like C) into machine code
that can be executed by a computer.
4. What is a linker?

○ A linker combines object files generated by the compiler into an


executable program.

Structure of a C Program

5. What are the basic components of a C program?

○ Preprocessor directives
○ Global declarations
○ Main function
○ User-defined functions
6. Explain the role of the main() function.

○ The main() function is the entry point of a C program. Execution


begins from the main() function.
7. What are comments in C?

○ Comments are used to explain code and improve readability.


○ Single-line comments: //
○ Multi-line comments: /* */

Data Types

8. What are the basic data types in C?

○ int: Integer
○ float: Floating-point number
○ double: Double-precision floating-point number
○ char: Character
9. What are keywords in C?

○ Keywords are reserved words with predefined meanings.


Examples: int, float, char, if, else, for, while, etc.
10. What are variables and constants?

● 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.

○ printf() is used to print formatted output to the console.


○ Syntax: printf(format_string, argument_list);
○ Format specifiers: %d (integer), %f (float), %c (character), %s
(string), etc.
3. Explain the scanf() function.

○ scanf() is used to read formatted input from the console.


○ Syntax: scanf(format_string, &variable1, &variable2, ...);
○ Format specifiers are similar to printf().
4. What is the difference between getchar() and getc()?

○ getchar() reads a single character from the standard input


stream (usually the keyboard).
○ getc() reads a single character from a specified file stream.
5. What is the difference between putchar() and putc()?

○ putchar() writes a single character to the standard output


stream (usually the console).
○ putc() writes a single character to a specified file stream.

Formatted Input/Output

6. What are format specifiers?

○ Format specifiers are used to specify the type of data to be read


or written.
7. How do you read and write strings using scanf() and printf()?

○ Use the %s format specifier.


○ Be careful with string input using scanf(), as it can lead to buffer
overflow if the input is too long.
8. How do you read and write floating-point numbers with specific
precision?

○ Use the %.nf format specifier, where n is the number of decimal


places.

File Input/Output

9. What are the basic file operations in C?

○ Opening a file, reading from a file, writing to a file, and closing a


file.
10. Explain the fopen() function.

● fopen() is used to open a file.


● Syntax: FILE *fp = fopen(filename, mode);
● Modes: r (read), w (write), a (append), r+ (read and write), w+ (write and
read), a+ (append and read)
11. Explain the fclose() function.
● fclose() is used to close a file.
● Syntax: fclose(fp);
12. Explain the fprintf() and fscanf() functions.
● fprintf() and fscanf() are used for formatted input and output
to/from files.
● Syntax is similar to printf() and scanf(), but the first argument is a
file pointer.

You might also like