THEORY QUESTIONS OF COMPUTER
Explain different file handling functions in C with examples.
The most commonly used function in file handling in C:
FUNCTIONS DESCRIPTION OF FUNCTION
fopen() Opening files either new or existing one
fclose() Closing or flushing the buffer of a program file
fscanf() Reading data stored in a file
fprintf() Writing data into available file
fread() Reading data stored in binary file
fwrite() Writing data into binary file
fgetc() Reading a single character from a file at a time
fputc() Writing a single character into available file
fgetw() Reading an integer from a file
fputw() Writing an integer into a file
fseek() Used to move or shift file pointer to a specified position
rewind() sets the file pointer to the beginning of the file
What is a file? Why do we use files in programming? What are the different
file-opening modes in C programming?
A file is a place on the disk where group of related data is stored. The data
file allows us to store the information permanently and to access and alter
the information whenever necessary. Why files are needed?
• When a program is terminated, the entire data is lost. Storing in a
file will preserve your data even if the program terminates.
• If you have to enter a large number of data, it will take a lot of time
to enter them all. However, if you have a file containing all the
data, you can easily access the contents of the file using a few
commands in C.
• You can easily move your data from one computer to another
without any changes.
Importance of file handling in C:
• Reusability
• Saves Time
• Portability
C programming language supports two types of files and they are as
follows...
• Text Files (or) ASCII Files.......................................... .txt
• Binary Files............................................................... .dat or .ree
• File opening mode:
Mode Meaning of Mode During Inexistence of file
r Open for reading. If the file does not exist, fopen() returns NULL.
rb Open for reading in binary mode. If the file does not exist, fopen() returns NULL.
w Open for writing.
If the file exists, its contents are overwritten. If
the file does not exist, it will be created.
wb Open for writing in binary mode.
If the file exists, its contents are overwritten. If
the file does not exist, it will be created.
a If the file does not exist, it will be created.
Open for append.
Data is added to the end of the file.
ab If the file does not exist, it will be created.
Open for append in binary mode.
Data is added to the end of the file.
r+ Open for both reading and writing. If the file does not exist, fopen() returns NULL.
rb+ If the file does not exist, fopen() returns NULL.
Open for both reading and writing in binary
mode.
w+ Open for both reading and writing.
If the file exists, its contents are overwritten. If
the file does not exist, it will be created.
wb+
Open for both reading and writing in binary If the file exists, its contents are overwritten. If
mode. the file does not exist, it will be created.
a+ Open for both reading and appending. If the file does not exist, it will be created.
ab+ If the file does not exist, it will be created.
Open for both reading and appending in
binary mode.
What is dynamic memory allocation (DMA)? Why is it needed? What are the
different functions used for dynamic memory allocation in C?
DMA (Dynamic Memory Allocation):-
The process of allocating and freeing memory at run time is known as DMA.
Dynamic memory allocation in C allows you to allocate memory at runtime
rather than at compile time. This means that you can allocate memory for
variables and data structures as needed during the execution of your
program.
In C, there are four library functions that you can use to allocate and
deallocate dynamic memory. You can use these library functions by adding
header file <stdlib.h>.
1. malloc( )
2. calloc( )
3. free( )
4. realloc( )
1. malloc( )
The name "malloc" stands for memory allocation.
This function is used to allocate a block of memory of a specified number
of bytes. And, it returns a pointer of (void) which can be casted into
pointers of any form.
Syntax: ptr = (cast type*)
malloc(no.of block* size of each
block); Eg:
float *ptr;
ptr = (float*)malloc(100*sizeof(float));
The above statement allocates 400 bytes of memory. It's because the size
of float is 4 bytes (100*4=400). And, the pointer ptr holds the address of
the first byte in the allocated memory.
The expression results in a NULL pointer if the memory cannot be
allocated.
2. calloc( )
The name "calloc" stands for contiguous allocation.
Another DMA function which is used for memory allocation of desired
datatype of n bolcks with equal size and returns first byte address
assigned to pointer. It initializes all them to zero.
Syntax :
ptr = (castType*)calloc(no.of block, size
of each block); Example:
int *ptr;
ptr = (int*) calloc(25, sizeof(int));
The above statement allocates contiguous space in memory for 25 elements of type int.
3. free( )
Dynamically allocated memory created with either calloc( ) or malloc( )
doesn't get freed on their own. You must explicitly use free( ) to release
the space. The function free previously allocated memory space by malloc(
), calloc( ) function.
Syntax:
free(ptr);
This statement frees the space allocated in the memory pointed by ptr.
4. realloc( ):
This function is used to modify the size of perviously allocated memory
space. It originally allocation is done by,
ptr = (float
*)malloc(100*size
of(float)); Then
reallocation can
be done by, ptr
=realloc(ptr,200*
sizeof(float));
Here, ptr is reallocated with a new size (200*4=800).
What are the different methods of passing an array to a function in C?
Explain with examples
Passing Array to Function in C
In C, there are various general problems which require passing more than
one variable of the same type to a function. For example, consider a
function which sorts the 10 elements in ascending order. Such a function
requires 10 numbers to be passed as the actual parameters from the main
function. Here, instead of declaring 10 different numbers and then passing
into the function, we can declare and initialize an array and pass that into
the function. This will resolve all the complexity since the function will now
work for any number of values.
Consider the following syntax to pass an array to the function.
functionname(arrayname);
Methods to declare a function that receives an array as an
argument
There are 3 ways to declare the function which is intended to receive an array as an argument.
First way: return_type
function(datatype
arrayname[]) ;
Declaring blank subscript notation [] is the widely used technique.
Second way:
return_type function(datatype
arrayname[SIZE],) ; Optionally, we can
define size in subscript notation [].
Third way:
return_type
function(datatype
*arrayname);
You can also use the concept of a pointer. In pointer chapter, we will learn about it.
Differentiate between structures and unions.
Union:
Union is also like structure, i.e. collection of different data types which are
grouped together.
Each element in a union is called member.
• Union and structure in C are same in concepts, except allocating
memory for their members.
• Structure allocates storage space for all its members separately.
Whereas, Union allocates one common storage space for all its
members
• We can access only one member of union at a time. We can't
access all member values at the same time in union. But, structure
can access all member values at the same time. This is because;
Union allocates one common storage space for all its members.
Whereas structure allocates storage space for all its members
separately.
• union keyword is used to declare and define union Syntax:
• DEFINE UNION
Union_name
{
Data_type variable_name1 data;
Data_type variable_name2 data;
Variable_name3;
}u
Declaring union variable:
Syntax: union
union_name
var_name;
Example:
union student
{ int mark; char name[20];
char add[20];
};
int main()
{
union student s;
}
Structure:
Structure is a collection of dissimilar or similar data types (different data
types) variable under a common name. A structure is user defined data type
in C. A structure creates a data type that can be used to group items of
possibly different types into a single name.
• C Structure is a collection of different data types which are grouped
together and each element in a C Structure is called member.
• If you want to access structure members in C, Structure variable
should be declared.
• Many structure variables can be declared for same structure and
memory will be allocated for each separately.
Define a structure:
Syntax:
struct structurename
{
datatype
membervariabl
e1; datatype
membervariabl
e2; datatype
membervariabl
e3;
…..
…..
….
datatype membervariablen;
};
Declare a structure variable:
Syntax:
OR
struct structurename struct structurename
{ {
datatype membervariable1; datatype datatype membervariable1; datatype
membervariable2; datatype membervariable2; datatype
membervariable3; membervariable3;
….. …..
….. …..
….. …..
datatype membervariablen; datatype membervariablen;
} variable1, variable2, ……..; };
int main()
{
struct structurename variable1, variable2, ……;
. Differentiate between call by value and call by reference with
proper examples.
1) Call by value:
If the function is called by passing the values of variable as actual
argument, the function is called call by value.
In this call value of each actual argument gets copied into formal
arguments of our function definition.
The content of calling function is not changed, Even if they are
changed in called function.
Pass value as argument
) Call by reference:
If the function is called by passing the address of variable as actual
argument, the function is called call by reference.
In this call, address of each actual argument gets copied into
formal argument of function definition.
The content of calling function is also changed, if any changed in
called function.
Pointer is used as formal argument in call by reference.
Pass address as argument. Eg:
What is a function? What are the advantages of using functions? Explain with
examples.
Introduction:
Function is a block of code or statement to perform some specific
(particular) task. Every c program has at least one function, which is main()
and all other trivial program can define additional function.
We can divide up our code into separate function, but logically the division
in such way that each function performs a specific task.
A single function can be accessed or called from different places with in a
program. After execution of the intended task, the control will be returned
to the point from which the function was accessed or called.
int main()
{ Int main()
------------ {
………….. -------------
………….. abc ();
…………. --------------
------------ code written twice --------------
------------ --------------
------------ abc();
………….. }
…………… void abc()
{
……………
…………
}
…………
…………
}
Advantage of using functions:
• Functions increase code reusability by avoiding rewriting of the
same code over and over.
• If a program is divided into multiple functions then each function
can be independently developed. So, program development will be
easer
• The program debugging will be easier.
• Length of the program can be reduced by using the function at
appropriate place.
• Function reduce program complexity
• Program development will be faster
• With function, it will be easier to understand the logic involved in
the program.
• Recursive call is possible through function (A function is allowed to
call itself. A function which calls itself directly or indirectly again
and again until specified condition is satisfied is known as recursive
function.)
Function increases program readability (clarity)
Differentiate between library and user-defined functions.
Difference between Library and user defined function:
Library function User-defined function
1) The library function is predefined 1) A user-defined function is not a
function in a header file pre-processor predefined function, it is defined by the
directive programmer according to the need.
2) The programmer can simply use this 2) The programmer has to declare define
function by including the respective header and use this function by them itself.
file.
3) Program development time will be faster. 3) Program development time will be usually
slower
4) The program using the library function 4) The program using a user-defined
will be usually simple function will be usually complex.
5) This function requires a header file to use 5) This function requires a function
it. prototype to use it.
6) Example:- printf(), scanf(), getch(), 6) Example:- Sum(), area(),factorial() etc.
strlen() etc.
Explain the types of user-defined functions with examples.
User-defined function
1) A user-defined function is not a predefined function, it is defined by the programmer
according to the need.
2) The programmer has to declare define and use this function by them itself.
3) Program development time will be usually slower
4) The program using a user-defined function will be usually complex.
5) This function requires a function prototype to use it.
6) Example:- Sum(), area(),factorial() etc.
Component of user defined function:
• Function declaration / function prototype
• Function call
• Function definition
1) Function declaration / function prototype:
Function prototype tells the compiler about the name of function, the type
of data returned by the function, the number type and order of
parameter.
Syntax:
returntype functionname(datatype variable1, datatype
variable2,..........); Eg:
void(int a, int b);
{
int add(int a, int b);
n);
This tells compiler that:-
• Name of function is add
• Function takes two arguments each of integer type.
• This function returns value whose type is integer.
Note: The return type "void" tells the compiler that it does not return any
value.
2) Function call:
Once function definition is ready, it must be called inside boundary of
main function. The function never comes in action if it is not called.
During function call, function name must be matched with function
prototype name and the number and order of argument.
Synatx:
functionname(argument1,argument2,………);
3) Function definition:-
The collection of program statement that describes the specific task to be
done by the function is called function definition.
Function definition consists of function header which defines function
name returns type and its arguments and consist of function body
which in enclosed in curly braces. Syntax:-
returntype functionname(datatype variable1, datatype variable2, …….)
{
---------------------------
-------------
----------------------------------------
function body --------------------------
-------------- }