You are on page 1of 24

QUESTION BANK

SUBJECT :ADVANCE PROGRAMMING IN ‘C’


Q.1 .Multiple choice questions.
1. Which of the following are C preprocessors?
a) #ifdef
b) #define
c) #endif
d) All of the mentioned
Answer:d
2. #include statement must be written
a) Before main()
b) Before any scanf/printf
c) After main()
d) It can be written anywhere
Answer:b
5. The C-preprocessors are specified with _________symbol.
a) #
b) $
c) ” ”
d) None of the mentioned
Answer:a
6. The #include directive
a) Tells the preprocessor to grab the text of a file and place it directly into the
current file
b) Statements are typically placed at the top of a program
c) both a & b
d) None of a & b
Answer:c
7. The preprocessor provides the ability for _______________.
a) The inclusion of header files
b) The inclusion of macro expansions
c) Conditional compilation and line control.
d) All of the mentioned
Answer:d
8. If #include is used with file name in angular brackets
a) The file is searched for in the standard compiler include paths
b) The search path is expanded to include the current source directory
c) Both a & b
d) None of the mentioned
Answer:a

9.How will you free the allocated memory ?


a)remove(var-name); b)free(var-name);
c)delete(var-name); d)dalloc(var-name);
Answer: b

10.What is the similarity between a structure, union and enumeration?


A. All of them let you define new values
B. All of them let you define new data types
C. All of them let you define new pointers
D. All of them let you define new structures

Answer:  B

Q .2 What is function ? Explain different types of functions.

Ans : Types of C functions

Basically, there are two types of functions in C on basis of whether it is defined


by user or not.

 Library function
 User defined function

Library function

Library functions are the in-built function in C programming system. Function


prototype and data definitions of these functions are written in their respective
header file. For example: If you want to use printf() function, the header file
<stdio.h> should be included. For example:

main(),printf(),scanf()

There are many library functions available in C programming to help the


programmer to write a good efficient program. you can find the square root by
just using sqrt() function which is defined under header file "math.h"Use
Of Library Function To Find Square root.

#include <stdio.h>
#include <math.h>
Void main()
{
float num,root;
printf("Enter a number to find square root.");
scanf("%f",&num);
root=sqrt(num); /* Computes the square root of num and stores in root. */
printf("Square root of %.2f=%.2f",num,root);
getch();
}
Library functions under "math.h"
Function Work of function
acos Computes arc cosine of the argument
acosh Computes hyperbolic arc cosine of the argument
asin Computes arc sine of the argument
asinh Computes hyperbolic arc sine of the argument
atan Computes arc tangent of the argument
atanh Computes hyperbolic arc tangent of the argument
atan2 Computes arc tangent and determine the quadrant using sign
cbrt Computes cube root of the argument
ceil Returns nearest integer greater than argument passed
cos Computes the cosine of the argument
cosh Computes the hyperbolic cosine of the argument
exp Computes the e raised to given power
fabs Computes absolute argument of floating point argument
floor Returns nearest integer lower than the argument passed.
Computes square root of sum of two arguments (Computes
hypot
hypotenuse)
log Computes natural logarithm
log10 Computes logarithm of base argument 10
pow Computes the number raised to given power
sin Computes sine of the argument
sinh Computes hyperbolic sine of the argument
sqrt Computes square root of the argument
tan Computes tangent of the argument
tanh Computes hyperbolic tangent of the argument

Library functions under "ctype.h"

Function Work Of Function

isalnum Tests whether a character is alphanumeric or not

isalpha Tests whether a character is aplhabetic or not

iscntrl Tests whether a character is control or not

isdigit Tests whether a character is digit or not


Library functions under "math.h"
Function Work of function
isgraph Tests whether a character is grahic or not

islower Tests whether a character is lowercase or not

isprint Tests whether a character is printable or not

ispunct Tests whether a character is punctuation or not

isspace Tests whether a character is white space or not

isupper Tests whether a character is uppercase or not

isxdigit Tests whether a character is hexadecimal or not

tolower Converts to lowercase if the character is in uppercase

toupper Converts to uppercase if the character is in lowercase

Q.3 What is user-defined function? Explain.

User defined function

C provides programmer to define their own function according to their


requirement known as user defined functions.

Elements of function:

1.Function call

Control of the program cannot be transferred to user-defined function unless it is


called invoked).

Syntax :function_name(argument(1),....argument(n));

In the above example, function call is made using statement


add(num1,num2); from main(). This make the control of program jump
from that statement to function definition and executes the codes inside that
function.

2.Function declaration

Function prototype(declaration):

Every function in C programming should be declared before they are used. These
type of declaration are also called function prototype. Function prototype gives
compiler information about function name, type of arguments to be passed and
return type.

Syntax of function prototype

return_type function_name(type(1) argument(1),....,type(n) argument(n));

In the above example,int add(int a, int b); is a function prototype


which provides following information to the compiler:

1. name of the function is add()


2. return type of the function is int.
3. two arguments of type int are passed to function.

Function prototype are not needed if user-definition function is written before


main() function.

3.Function Definition

Function definition contains programming codes to perform specific task.

Syntax :
return_type function_name(type(1) argument(1),..,type(n) argument(n))
{
//body of function
}

Function definition has two major components:

1. Function declarator

Function declarator is the first line of function definition. When a function is


invoked from calling function, control of the program is transferred to function
declarator or called function.

Syntax :
return_type function_name(type(1) argument(1),....,type(n) argument(n))

Syntax of function declaration and declarator are almost same except, there is no
semicolon at the end of declarator and function declarator is followed by
function body.

In above example, int add(int a,int b) in line 12 is a function


declarator.

2. Function body

Function declarator is followed by body of function which is composed of


statements.
Passing arguments to functions

In programming, argument/parameter is a piece of data(constant or  variable)


passed from a program to the function.

Return Statement

Return statement is used for returning a value from function definition to calling
function.

Syntax of return statement

return (expression);
OR
return;
Syntax:
#include <stdio.h>
void function_name(){
................
................
}
int main(){
...........
...........
function_name();
...........
...........
}
Ex.1 A sample program of user-defined functions.
#include<stdio.h>
void print(void);
int main()
{
int i;
for(i=0;i<5;i++)
{
print();
printf("\n");
}

getch();
}
void print()
{
printf("Welcome to c");
}
Ex.2 Write a C program to add two integers. Make a function add to add
integers and display sum in main() function.

/*Program to demonstrate the working of user defined function*/


#include <stdio.h>
int add(int a, int b); //function prototype(declaration)
int main(){
int num1,num2,sum;
printf("Enters two number to add\n");
scanf("%d %d",&num1,&num2);
sum=add(num1,num2); //function call
printf("sum=%d",sum);
return 0;
}
int add(int a,int b) //function declarator
{
/* Start of function definition. */
int add;
add=a+b;
return add; //return statement of function
/* End of function definition. */
}
EX.3. Write a program to accept an integer value. Pass this value to
two functions - one to compute square value, and the other to
compute cube value.
#include<stdio.h>
int square(int);
int cube(int);
int main()
{
int a;
printf("Enter a number :");
scanf("%d",&a);
int b=square(a);
printf("\nSquare= %d",b);
b=cube(a);
printf("\nCube= %d",b);
getch();
}
int square(int x)
{
return x*x;
}
int cube(int x)
{
return x*x*x;
}
Q .4 What are the advantages of user-defined function?

Advantages of user defined functions:

1.User defined functions helps to decompose the large program into small
segments which makes programmar easy to understand, maintain and debug.

2.If repeated code occurs in a program. Function can be used to include those
codes and execute when needed by calling that function.

3.Programmar working on large project can divide the workload by making


different functions.

Q .5. What is recursive function? OR What is recursion?

Ans: A function that calls itself is known as recursive function and the process
of calling function itself is known as recursion in C programming.

Example of recursion in C programming

Write a C program to find sum of first n natural numbers using recursion. Note:
Positive integers are known as natural number i.e. 1, 2, 3....n

#include <stdio.h>
int sum(int n);
int main(){
int num,add;
printf("Enter a positive integer:\n");
scanf("%d",&num);
add=sum(num);
printf("sum=%d",add);
}
int sum(int n){
if(n==0)
return n;
else
return n+sum(n-1); /*self call to function sum() */
}

Output

Enter a positive integer:


5
15

In, this program, sum() function is invoked from the same function. If n is not
equal to 0 then, the function calls itself passing argument 1 less than the previous
argument it was called with. Suppose, n is 5 initially. Then, during next function
calls, 4 is passed to function and the value of argument decreases by 1 in each
recursive call. When, n becomes equal to 0, the value of n is returned which is
the sum numbers from 5 to 1.

For better visualization of recursion in this example:

sum(5)
=5+sum(4)
=5+4+sum(3)
=5+4+3+sum(2)
=5+4+3+2+sum(1)
=5+4+3+2+1+sum(0)
=5+4+3+2+1+0
=5+4+3+2+1
=5+4+3+3
=5+4+6
=5+10
=15

Every recursive function must be provided with a way to end the recursion. In
this example when, n is equal to 0, there is no recursive call and recursion ends.

Q. 6 What are the advantages and disadvantages of recursion?

Ans:Advantages and Disadvantages of Recursion

Recursion is more elegant and requires few variables which make program clean.
Recursion can be used to replace complex nesting code by dividing the problem
into same problem of its sub-type.

In other hand, it is hard to think the logic of a recursive function. It is also


difficult to debug the code containing recursion.

Q.7 Write a program in C to Calculate Factorial of a Number Using Recursion


/* P
#include<stdio.h>
int factorial(int n);
void main()
{
int n;
printf("Enter an positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, factorial(n));
getch();
}
int factorial(int n)
{
if(n!=1)
return n*factorial(n-1);
}

Output

Enter an positive integer: 6


Factorial of 6 = 720

Q.8.Explain the different storage classes in ‘C’.


Ans: Every variable and function in C programming has two properties:
type and storage class. Type refers to the data type of variable whether it
is character or integer or floating-point value etc.
There are 4 types of storage class:
1. automatic
2. external
3. static
4. register
Automatoic storage class
Variables declared inside the function body are automatic by default.
These variable are also known as local variables as they are local to the
function and doesn't have meaning outside that function
Since, variable inside a function is automatic by default, keyword auto
are rarely used.
Eg. Int n;
External storage class
External variable can be accessed by any function. They are also known
as global variables. Variables declared outside every function are
external variables.
In case of large program, containing more than one file, if the global
variable is declared in file 1 and that variable is used in file 2 then,
compiler will show error. To solve this problem, keyword extern is used
in file 2 to indicate that, the variable specified is global variable and
declared in another file.
Example to demonstrate working of external variable
#include<stdio.h>
void Check();
int a=5; /* a is global variable because it is outside every function */
int main()
{
a+=4;/* same as a=a+4*/
Check();
return 0;
}
void Check()
{
++a; /* ----- Variable a is not declared in this function but, works in
any function as they are global variable ------- */
printf("a=%d\n",a);
}
Output
a=10
Register Storage Class
register int a;
Register variables are similar to automatic variable and exists inside that
particular function only.
If the compiler encounters register variable, it tries to store variable in
microprocessor's register rather than memory. Value stored in register
are much faster than that of memory.
In case of larger program, variables that are used in loops and function
parameters are declared register variables.
Since, there are limited number of register in processor and if it couldn't
store the variable in register, it will automatically store it in memory.
Static Storage Class
The value of static variable persists until the end of the program. A
variable can be declared static using keyword: static. For example:
static int i;
Here, i is a static variable.
Example to demonstrate the static variable
#include <stdio.h>
void Check();
int main(){
Check();
Check();
Check();
}
void Check(){
static int count=0;
printf("%d\t",count);
count+=5;
}
Output
0 5 10
During first function call, it will display 0. Then, during second function
call, variable c will not be initialized to 0 again, as it is static variable.
So, 5 is displayed in second function call and 10 in third call.
If variable c had been automatic variable, the output would have
been:
0 0 0

Q. 11 Explain how you can define the data by using ‘typedef’ statement ?Give
the suitable example.
Ans: Typedef is used to define new data type for an existing data type. It
provides an alternativename for standard data type. It is used for self
documenting the code by allowingdescriptive names. (for beltes understanding)
for the standard data type. The C programming language provides a keyword
called typedef, which you can use to give a type a new name.
The general format is:
typedef existing datatype new datatype;
For example:
typedef float real;
Now, in a program one can use datatype real instead of float.
Therefore, the following statement is valid:
real amount;
After this type definitions, the identifier BYTE can be used as an abbreviation
for the type unsigned char, for example:.
By convention, uppercase letters are used for these definitions to remind the
user that the type name is really a symbolic abbreviation, but you can use
lowercase, as follows:

typedef unsigned char byte;


You can use typedef to give a name to user defined data type as well. For
example you can use typedef with structure to define a new data type and then
use that data type to define structure variables directly as follows:
// Structure using typedef:
 
#include <stdio.h>
#include <string.h>
 
typedef struct student
{
  int id;
  char name[20];
  float percentage;
} status;
 
int main()
{
  status record;
  record.id=1;
  strcpy(record.name, "Raju");
  record.percentage = 86.5;
  printf(" Id is: %d \n", record.id);
  printf(" Name is: %s \n", record.name);
  printf(" Percentage is: %f \n", record.percentage);
  return 0;
}

Q.12 Explain in brief the use of enumerated data types with example
Ans: Enumerated Data Type
It has the following features:
 It is user defined.
 It works if you know in advance a finite list of values that a data type can take.
 The list cannot be input by the user or output on the screen.
For example:
enum months { jan, feb, mar, apr, may};
enum days { sun, mon, tue, wed, thu };
enum toys { cycle, bicycle, scooter };
The enum specifier defines the set of all names that will be permissible values
of
the type called members which are stored internally as integer constant. The
first
name was given the integer value 0, the second value 1 and so on.
#include< stdio.h>
void main()
{
int i;
enum month {JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,DEC};
clrscr();
for(i=JAN;i<=DEC;i++)
printf("\n%d",i);
}
Output :
01234567891011
Q.13 Explain in brief arithmetic operations perform on pointers.OR pointer
arithmetic

You can perform a limited number of arithmetic operations on pointers. These


operations are:

 Increment and decrement


 Addition and subtraction
 Comparison
 Assignment

The increment (++) operator increases the value of a pointer by the size of the
data object the pointer refers to. For example, if the pointer refers to the second
element in an array, the ++ makes the pointer refer to the third element in the
array.
The decrement (--) operator decreases the value of a pointer by the size of the
data object the pointer refers to. For example, if the pointer refers to the second
element in an array, the -- makes the pointer refer to the first element in the
array.

You can add an integer to a pointer but you cannot add a pointer to a pointer.

If the pointer p points to the first element in an array, the following expression


causes the pointer to point to the third element in the same array:

p = p + 2;

If you have two pointers that point to the same array, you can subtract one
pointer from the other. This operation yields the number of elements in the array
that separate the two addresses that the pointers refer to.

You can compare two pointers with the following operators: ==, !=, <, >, <=,


and >=.

Program to illustrate the use of pointers in arithmetic operations in C


Programming

main()
{
int a, b, *p1, *p2, x, y, z;
a = 12;
b = 4;
p1 = &a;
p2 = &b;
x = *p1 * *p2 - 6;
y = 4* - *p2 / *p1 + 10;
printf("Address of a = %u\n", p1);
printf("Address of b = %u\n", p2);
printf("\n");
printf("a = %d, b = %d\n", a, b);
printf("x = %d, y = %d\n", x, y);
*p2 = *p2 + 3;
*p1 = *p2 - 5;
z = *p1 * *p2 - 6;
printf("\na = %d, b = %d,", a, b);
printf(" z = %d\n", z);
}

Output :
Address of a = 4020
Address of b = 4016
a = 12, b = 4
x = 42, y = 9
a = 2, b = 7, z = 8

Q .14 Write a C program to create a file called emp.rec and store


information
  about a person
Ans: /*C program to create a file called emp.rec and store information
  about a person, in terms of his name, age and salary.*/
#include <stdio.h>
void main()
{
FILE *fp;
char name[20];
int age;
float salary;

/* open for writing */


fp = fopen("emp.rec", "w");

if (fp == NULL)
{
printf("File does not exists \n");
return;
}
printf("Enter the name \n");
scanf("%s", name);
fprintf(fp, "Name = %s\n", name);
printf("Enter the age\n");
scanf("%d", &age);
fprintf(fp, "Age = %d\n", age);
printf("Enter the salary\n");
scanf("%f", &salary);
fprintf(fp, "Salary = %.2f\n", salary);
fclose(fp);
}

Enter the name


raj
Enter the age
40
Enter the salary
4000000

Q.15 What is a structure?Explain.


OR
What is the syntax of accessing data members of the structure?
Ans: Structure is the collection of variables of different types under a
single name for better handling. For example: You want to store the
information about person about his/her name, citizenship number and
salary. You can create these information separately but, better approach
will be collection of these information under single name because all
these information are related to person.
Keyword struct is used for creating a structure.
Syntax of structure
struct structure_name
{ data_type member1;
data_type member2;
.
.
data_type memeber;
};
We can create the structure for a person as mentioned above as:
struct person
{
char name[50];
int cit_no;
float salary;
};
This declaration above creates the derived data type struct person.
Structure variable declaration
When a structure is defined, it creates a user-defined type but, no storage
is allocated. For the above structure of person, variable can be declared
as:
struct person
{
char name[50];
int cit_no;
float salary;
};
main()
{ /*Inside main function*/
struct person p1, p2, p[20];
}
Another way of creating sturcture variable is:
struct person
{
char name[50];
int cit_no;
float salary;
}p1 ,p2 ,p[20];
In both cases, 2 variables p1, p2 and array p having 20 elements of
type struct person are created.
Accessing members of a structure
There are two types of operators used for accessing members of a
structure.
1. Member operator(.)
2. Structure pointer operator(->) (will be discussed in structure and
pointers chapter )
Any member of a structure can be accessed
as: structure_variable_name.member_name
Suppose, we want to access salary for variable p2. Then, it can be
accessed as:p2.salary
Example of structure to Store Information of Single student
#include <stdio.h>
struct student{
char name[50];
int roll;
float marks;
};
int main()
{
struct student s;
printf("Enter information of students:\n\n");
printf("Enter name: ");
scanf("%s",s.name);
printf("Enter roll number: ");
scanf("%d",&s.roll);
printf("Enter marks: ");
scanf("%f",&s.marks);
printf("\nDisplaying Information\n");
printf("Name: %s\n",s.name);
printf("Roll number: %d\n",s.roll);
printf("Marks: %.2f\n",s.marks);
return 0;
}
Output:
Enter information of students:
Enter name:Neha
Enter roll number:5
Enter marks:85.2
Displaying information
Name:Neha
Roll number:5
Marks:85.2
Q.16 What is array of structure?Explain with example.
Ans: Structure is used to store information of one particular object but if
we need to store information of number of objects then array of structure
is used.
Example to Store Information of 10 students Using array of Structure
#include <stdio.h>
struct student{
char name[50];
int roll;
float marks;
};
void main()
{
struct student s[10];
int i;
printf("Enter information of students:\n");
for(i=1;i<=10;i++)
{
printf("\nFor roll number %d\n",i);
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter marks: ");
scanf("%f",&s[i].marks);
printf("\n");
}
printf(" Displaying information of students:\n\n");
for(i=1;i<=10;i++)
{
printf("\n Information for roll number %d:\n",i);
printf("Name: ");
puts(s[i].name);
printf("Marks: %.1f",s[i].marks);
}
getch();
}
Output
Enter information of students:

For roll number 1


Enter name: Sneha
Enter marks: 98
For roll number 2
Enter name: Varsha
Enter marks: 89
.
.
.
Displaying information of students:

Information for roll number 1:


Name: Sneha
Marks: 98
.
.
.
Q.17 How is structure different from array? OR Differentiate between structure
and array.

Ans: Both the arrays and structures are classified as structured data types as they provide a
mechanism that enable us to access and manipulate data in a relatively easy manner. But they
differ in a number of ways listed in table below:

Arrays Structures
1. An array is a collection of 1. Structure can have elements of
related data elements of same different  types
type.
2. An array is a derived data 2. A structure is a programmer-
type defined data type
3. Any array behaves like a 3. But in the case of structure,
built-in data types. All we have first we have to design and
to do is to declare an array declare a data structure before the
variable and use it. variable of that type are declared
and used.
Array allocates static memory Structures allocate dynamic
and uses index / subscript for memory and uses (.) operator for
accessing elements of the accessing the member of a
array.  structure.
Array is a base pointer.. Structure is not a pointer
** it points to a particular
memory location..

Q.18 What is nesting of structure?


Ans: Structures can be used as structures within structures. It is also called as
'nesting of structures'.
Syntax:

struct structure_nm
{
<data-type> element 1;
<data-type> element 2;
-----------
-----------
<data-type> element n;

struct structure_nm
{
<data-type> element 1;
<data-type> element 2;
-----------
-----------
<data-type> element n;
}inner_struct_var;
}outer_struct_var;

Example :

struct stud_Res
{
int rno;
char nm[50];
char std[10];

struct stud_subj
{
char subjnm[30];
int marks;
}subj;
}result;
In above example, the structure stud_Res consists of stud_subj which itself is a
structure with two members. Structure stud_Res is called as 'outer structure' while
stud_subj is called as 'inner structure.' The members which are inside the inner
structure can be accessed as follow :
result.subj.subjnm
result.subj.marks
/* Program to demonstrate nested structures.
#include <stdio.h>
#include <conio.h>

struct stud_Result
{
int rno;
char std[10];
struct stud_Marks
{
char subj_nm[30];
int subj_mark;
}marks;
}res;

void main()
{
clrscr();
printf("\n\t Enter Roll Number : ");
scanf("%d",&res.rno);
printf("\n\t Enter Standard : ");
scanf("%s",res.std);
printf("\n\t Enter Subject Code : ");
scanf("%s",result.marks.subj_nm);
printf("\n\t Enter Marks : ");
scanf("%d",&result.marks.subj_mark);
printf("\n\n\t Roll Number : %d",res.rno);
printf("\n\n\t Standard : %s",res.std);
printf("\nSubject Code : %s",res.marks.subj_nm);
printf("\n\n\t Marks : %d",res.marks.subj_mark);
getch();
}
Output :
Enter Roll Number : 1
Enter standard : B.Sc(CS)-I
Enter subject code : CS01
Enter marks : 80
Roll Number : 1
Standard : B.Sc(CS)-I
Subject code : CS01
Q.19 What is union? Explain.
Ans. A union is a special data type available in C that enables you to store different data types
in the same memory location. You can define a union with many members, but only one
member can contain a value at any given time. Unions provide an efficient way of using the
same memory location for multi-purpose.
Defining a Union
To define a union, you must use the union statement in very similar was as you did while
defining structure. The union statement defines a new data type, with more than one member
for your program. The format of the union statement is as follows:
union <union tag>
{
member definition;
member definition;
...
member definition;
} [one or more union variables];
Ex. union Data
{
int i;
float f;
char str[20];
} data;
Now, a variable of Data type can store an integer, a floating-point number, or a string of
characters. This means that a single variable ie. same memory location can be used to store
multiple types of data.

Accessing Union Members


To access any member of a union, we use the member access operator (.). The member access
operator is coded as a period between the union variable name and the union member that we
wish to access. You would use union keyword to define variables of union type. Following is
the example to explain usage of union:
The memory occupied by a union will be large enough to hold the largest member of the union.
For example, in above example Data type will occupy 20 bytes of memory space because this
is the maximum space which can be occupied by character string. Following is the example
which will display total memory size occupied by the above union:

#include <stdio.h>
#include <string.h>

union Data
{
int i;
float f;
char str[20];
};
void main( )
{
union Data data;

data.i = 10;
data.f = 220.5;
strcpy( data.str, "C Programming");

printf( "data.i : %d\n", data.i);


printf( "data.f : %f\n", data.f);
printf( "data.str : %s\n", data.str);
getch();
}
Q.20 What is the difference Between Structure and Union

Structure Union

i. Access Members     
We can access all the members of structure at
Only one member of union can be accessed at anytime.
anytime.

ii. Memory Allocation     

Allocates memory for variable which variable require more


Memory is allocated for all variables.
memory.

iii. Initialization     

All members of structure can be initialized Only the first member of a union can be initialized.

iv. Keyword     

'struct' keyword is used to declare structure. 'union' keyword is used to declare union.

v. Syntax     

struct struct_name union union_name


{ {
structure element 1; union element 1;
structure element 2; union element 2;
---------- ----------
---------- ----------
structure element n; union element n;
}struct_var_nm; }union_var_nm;
vi. Example     

struct item_mst union item_mst


{ {
int rno; int rno;
char nm[50]; char nm[50];
}it; }it;

Q.21 What are the different modes of files?


Ans :The file modes are as follows,

Mode Description
r Opens an existing text file for reading purpose.
Opens a text file for writing, if it does not exist then a new file is created. Here
w
your program will start writing content from the beginning of the file.
Opens a text file for writing in appending mode, if it does not exist then a new
a file is created. Here your program will start appending content in the existing
file content.
r+ Opens a text file for reading and writing both.
Opens a text file for reading and writing both. It first truncate the file to zero
w+
length if it exists otherwise create the file if it does not exist.
Opens a text file for reading and writing both. It creates the file if it does not
a+ exist. The reading will start from the beginning but writing can only be
appended.

Q.22 Explain the following functions with proper syntax and example:
i)exit()
II)gets
III)puts
IV)flushall()
V)random()
VI)fprintf()
VII)abs()
VII)pow()
Q.23. Explain the following string handling functions:
i)strlen()
II)strcpy()
III)strcat()
IV)strupr()
V)strlwr()

Q.27

You might also like