You are on page 1of 8

Write differences between call by value and call reference.

Call by value Call reference


1. Only the value of 1. Address of arguments are
arguments are pass to the pass to the call function
call function
2. Pointer is not used 2. Pointer is used to pass
address of argument.
3. Values of variables are not 3. Change in the value of
affected while changing the formal parameter change
value of formal parameter. the value of the variable.
4. Argument can pass single 4. Argument can pass
value at a time to the call multiple values (value of
function. array, string) at a time to
the call function.
5. #include<stdio.h> 5. #include<stdio.h>
void func(int , int ); void func(int *, int *);
void main( ) void main( )
{ {
int a,b; int a,b;
a=1; b=2; a=1; b=2;
func(a,b); func(&a,&b);
} printf(“Now a=%d,b=%d”,a,b);
void func(int x,int y) }
{ void func(int x, int y)
int temp; {
temp=x; int temp;
x=y; temp=*x;
y=temp; *x=*y;
printf(“Now a=%d,b=%d”a,b); *y=temp;
} }

Differentiate between array and structure with example. [5]

Array Structure

1. Array stores groups of 1. Structure is used to store


same elements in group of different types of
consecutive memory data values in one common
locations under a name.
common name.

2. Array is a built-in data 2. Structure is a derived data


type. type.

3. We cannot have array of 3. We can have array of


array. structure.
3. For example: if we need to 3. For example if we need to
store the marks of 100 store logically related data
different students, then items like roll no, name and
we can create memory marks, they all are logically
space of size 100 under a related to students so to bind
single variable name. them together we can use
structure.

4. An array is declared with 4. A structure is designed at


following syntax : first and declared as follows:
Data_type array_name[size]; struct structure_name
{
Data_type member1;
Data_type member2;
…………………..
…………………..
}structure_variable_name;

5. There is not any special 5. There is struct keyword to


keyword to declare array declare the structure
variable. variable.

6. Array is simple data types 6. Structure are complex than


than structure. array.

7. The examples of array are 7. The example of structure is


int age[10]; struct student
float percent[10]; {
int rn;
char name[50];
int age;
};
struct student s;

What is function? List out the advantage of function. [1+4]


A function is a self continued block of code or sub programs that performs a particular tasks. In
other word, function is the process of breaking a large program into small manageable tasks and
designed them independently which is called modular programming. In C, function can be classified
into two types on the basis of definition. They are library function and user defined function. Library
function are also known as predefined function because the task that is to be done by function has
been already defined and user can't change the meaning of the function for other purpose. Example
of predefined function are printf( ), scanf( ), getch( ), clrscr( ), etc. whereas in user-defined function
user defined the task that has been done by the function. main( ) function is the example of user-
defined function. Once the function is declared and defined it can be called in main function or
other sub function for any time according to the requirement.
The advantages of functions are discussed below:
1. Generally a difficult problem is divided into small manageable form. A program can be divided
into function, each of which performs some specific tasks. So the use of function modularizes
and divides the work of a program.
2. Easy to read, write and debug function.
3. Easier to maintain or modify such functions.
4. Small function tends to be self-documenting and highly readable.
5. When some specific code is to be used many times at different places in the program the use of
functions avoid the repetition of that code i.e, it can be called any number of times in any place
with different parameters.

Write fscanf and fprintf function. [5]

fprintf( ) function is used to write the content of program


memory to the memory location of the file hold by the file
pointer.
Syntax:
fprintf(fp,"format specifier",arguments);
Here, fprintf( ) signifies that content of the arguments in
specific format get printed in the memory address of a file
hold by the file pointer where arguments are one or more
than one valid variables with value. So, it is a write
function. It is used when we have to write the content in the
file in text mode.

fscanf( ) function is used to read the content from the file in


program memory.
Syntax:
fscanf(fp,"format-specifier",arguments);
Here, fscanf( ) signifies that content of file that is hold by the
file pointer is stored in the program memory in specific
format. It is used when we have to read the content of the
file that is in text mode to the program memory.

Differentiate between structure and union with suitable examples. [10]

Structure Union

1. Structure is declared by 1. Union is declared by using


using 'struct' keyword. 'union' keyword.
2. The syntax to design 2. The syntax to design union
structure is as follows is as follows:
struct structure_name union union_name
{ {
Data_type member1; Data_type member1;
Data_type member2; Data_type member2;
………………….. …………………..
………………….. …………………..
}structure_variable_name; }union_variable_name;

3. The sum of size of all 3. The size of memory


members represent the occupied by union will be
size of memory in as large as the member
structure. For ex: with largest size in union.
For ex:
struct student
union student
{
{
char name[20];
char name[30];
//char =1byte*20=20 byte
//char =1byte*30=30 byte
int phone[10];
int phone[50];
//int=2 byte*10=20 byte
//int=2 byte*50=100 byte
}
}
struct student s;
union student s;
The size of the above
structure is the sum of the The size of the above union
two members i.e. 40 bytes. variable is equal to the size
of largest member i.e. 100
bytes.

4. Elements under a 4. Elements under a union


structure variables are variables are dependent to
independent to each other each other.

5. Structure contains many 5. Union may contain many


members of different types members of different types
and can handle all but it can handle only one
member at once. member at a time.

6. Structure is used when 6. Union is used when memory


memory is large and have is less and have to store
to store values of all the value in one of the declared
variables. variables or members.

Example of structure: Program to input student name, rollno and marks of math, nepali and
computer then calculate total mark, percentage using structure variable.
#include<stdio.h>
#include<conio.h>
struct ram
{
char stuname[20];
int rollno;
float math,nep,com,tot,per;
};
void main( )
{
struct ram a;
printf(“Enter student name”);
gets(a.stuname);
printf(“Enter roll number”);
scanf(“%d”,&a.rollno);
printf(“Enter marks of math nepali and computer”);
scanf(“%f %f %f”, &a.math, &a.nep, &a.com);
a.tot=a.math+a.nep+a.com;
a.per=a.tot/3;
printf(“Your total number is %f”,a.tot);
printf(“percentage is %f”,a.per);
getch( );
}
Example of union: Program to input student name, rollno and marks of math, nepali and computer
then calculate total mark, percentage using union variable.
#include<stdio.h>
#include<conio.h>
union ram
{
char stuname[20];
int rollno;
float math, nep, com, tot, per;
};
void main( )
{
union ram a;
printf(“Enter student name”);
gets(a.stuname);
printf(“Enter roll number”);
scanf(“%d”,&a.rollno);
printf(“Enter number of math nepali and computer”);
scanf(“%f %f %f”, &a.math, &a.nep, &a.com);
a.tot=a.math+a.nep+a.com;
a.per=a.tot/3;
printf(“Your total number is %f”, a.tot);
printf(“percentage is %f”, a.per);
getch( );
}

Describe any five 'file handling function' with examples. [10]


In C program, all the data get stored in specific memory called program memory. The data stored in
program memory remains until the program gets executed and when program is terminated then
data also removed from program memory. But we have to store the data wherever we need without
destroy. This is possible only by file handling function which is available in C library. C supports a
member of function that have the ability to perform basic file operation like creating a new file,
reading, writing from/to a file, closing a file and so on. Some functions are discussed below:

Function Description

fopen( ) This function is used to open a data file in


different modes.
syntax:
fp=fopen("filename","mode");
Here, fp is any valid file pointer which can
store the memory address of a file in the
program memory and fopen function
opens the file "filename" in different modes
such as read, write, append and returns
the address of the opened file in the file
pointer fp.

fclose(fp) A file must be closed as soon as all


operations on it have been completed.
fclose( ) function is used to close the file
that is being opened in the program. It
means that fclose function is used to
remove the memory address of file that is
hold by a file pointer fp. This ensures that
all information associated with file is
flushed away that is hold by the file
pointer.
syntax:
fclose(fp);

fprintf( ) fprintf( ) function is used to write the


content of program memory to the
memory location of the file hold by the file
pointer.
Syntax:
fprintf(fp,"format specifier",arguments);
Here, fprintf( ) signifies that content of the
arguments in specific format get printed in
the memory address of a file hold by the
file pointer where arguments are one or
more than one valid variables with value.
So, it is a write function. It is used when
we have to write the content in the file in
text mode.

fscanf( ) fscanf( ) function is used to read the


content from the file in program memory.
Syntax:
fscanf(fp,"format-specifier",arguments);
Here, fscanf( ) signifies that content of file
that is hold by the file pointer is stored in
the program memory in specific format. It
is used when we have to read the content
of the file that is in text mode to the
program memory.

remove( ) This function is used to delete the file


from the computer memory.
Syntax:
remove("filename");

Example: Program to read records from data file student.txt and delete the record if its name
match with name "Ramesh".
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main( )
{
clrscr( );
FILE *f1,*f2;
char sname[20];
int i;
//code to delete the name that matches with name Ramesh
f1=fopen("student.txt","r");
f2=fopen("newrecord.txt","w");
while(fscanf(f1, "%s", name)==1)
{
printf("%s\n",sname);
if(strcmp(sname,"Ramesh")==0)
{
continue;
}
fprintf(f2, "%s", sname);
}
fclose(f1);
fclose(f2);
remove("studen.txt");
rename("newrecord.txt","student.txt");
getch( );
}

You might also like