Important questions with answers-1
Important questions with answers-1
1. Differentiate between call by value and call by reference using suitable example.
In Call by value method, the values of variables are passed by the calling function to the called function.
In Call by reference method, the address of variables is passed by the calling function to the called function.
Ex: Program to swap the value of 2 variables using call by value & call by reference methods:
#include<stdio.h>
void swap_call_by_value(int a, int b);
void swap_call_by_ref(int *c, int *d);
int main()
{
int a=1, b=2, c=3, d=4;
printf(“\n In main, a=%d & b=%d”,a,b);
swap_call_by_value(a, b);
printf(“\n In main, a=%d & b=%d”,a,b);
printf(“\n In main, c=%d & d=%d”,c,d);
swap_call_by_ref(&c, &d);
printf(“\n In main, c=%d & d=%d”,c,d);
return 0;
}
void swap_call_by_value(int a, int b);
{
int temp;
temp=a;
a=b;
b=temp;
printf(“\n In function, a=%d & b=%d”,a,b);
}
void swap_call_by_ref(int *c, int *d);
{
int temp;
temp=*c;
*c=*d;
*d=temp;
printf(“\n In function, c=%d & d=%d”,c,d);
}
Output:
In main, a=1 & b=2
In function, a=2 & b=1
In main, a=1 & b=2
In main, c=3 & d=4
In function, c=4 & d=3
In main, c=4 & d=3
Storage class
Feature
auto extern register static
Accessability within the within all within the Local: within the function
function or block program files function or block or block in which it is
in which it is that are a part in which it is declared
declared of a program declared Global: within the
program in which it is
declared
Storage Main memory Main memory CPU register Main memory
Existence when the function throughout the when the function Local: retains the value
or block in which execution of or block in which between function calls or
it is declared is the program it is declared is block entries
entered and entered and Global: preserves the
ceases to exist ceases to exist value in the program files
outside outside
Default value Garbage Zero Garbage Zero
4. Explain the declaration and initialization of one dimensional and two dimensional arrays with an
example.
One dimensional array is declared by specifying the (i) data type of the values it can store (ii) name to
identify the array, and (iii) size i.e, maximum number of values it can hold.
Syntax: type name[size];
Ex: int marks[5];
1st element 2nd element 3rd element 4th element 5th element
marks[0] marks[1] marks[2] marks[3] marks[4]
Initialization of one dimensional array: there are 3 ways to intialize i.e.,storing the values in an array:
(i) during declaration of the array :
type array_name[size]={list of values};
Examples: int marks[5]={90,82,78,95,88}; OR int marks[]={90,82,78,95,88};
(ii) input values for the elements from the key board:
Ex: int i, marks[5];
for(i=0;i<5;i++)
scanf(“%d”, &marks[i]);
(iii) asssign values to the individual elements.
Ex: marks[0]=90; marks[1]=82; etc.
Two dimensional array is declared as:
Syntax: data_type array_name[row_size] [column_size]
Ex: int marks[2][3];
1st element 2nd element 3rd element 4th element 5th element 6th element
marks[0][0] marks[0][1] marks[0][2] marks[1][0] marks[1][1] marks[1][2]
A two-dimensional m×n array contains m×n data elements and each element is accessed using teo subscripts
i and j where i<m and j<n.
Initialization of two dimensional array: the values are initialized i.e., stored in the two dimentional array
as follows - Ex: consider to store 2 students marks in 3 different subjects
(i) during declaration of the array :
int marks[2][3]={90,87,78,68,62,71};
OR int marks[2][3]={{98,87,78},{68,62,71};
(ii) input values for the elements from the key board:
Ex: int i, marks[2][3];
for(i=0;i<2;i++)
for(j=0;i<3;j++)
scanf(“%d”,&marks[i][j]);
(iii) asssign values to the individual elements.
Ex: marks[0][0]=90;
5. Define String. Explain any 2 string manipulation function with suitable example.
A string is a null terminated character array.
The general form of declaring string is as follows:
char str[size];
Ex: char str[10]=”HELLO”;
H E L L O \0 \0 \0 \0 \0
(ii) strcmp(str1, str2): This function compares the string pointed by str1 to the string pointed by str2. The
function returns zero if the strings are equal otherwise it returns a value less or greater than zero.
Ex: #include<stdio.h>
#include<string.h>
int main()
{
char str1[]=”Programm”;
char str2[]=”ing”;
if(strcmp(str1,str2)==0)
printf(“The two strings are identical”);
else
printf(“The two strings are not identical”);
return 0;
}
output: The two strings are not identical
Generic or void pointer: It is a pointer variable that has void as its data type. It is a special type of pointer
that can be used to point to variables of any data type.
Syntax: void *ptr;
Ex:
#include<stdio.h>
int main()
{
int x=10;
char ch=’A’;
void *gp;
gp=&x;
printf(“Generic pointer to the integer value = %d”, *(int*)gp);
gp=&ch;
printf(“\n Generic pointer to the character = %c”, *(char*)gp);
return 0;
}
Example shows the under structure student, 2 variables are declared i.e., stud1 and stud2, and also stud2 is
initialized with values for individual data members.
It can also be done for stud1like
struct student stud1={01,”Raju”, “BCA”,4500};
and structure variable declaration can be done also like
struct student stud3;
A structure members are accessed using a ‘.’ (dot) operator.
Syntax: struct_var.member_name;
To assign values: stud1.name=”Rahul”
stud1.fees=45000;
To input the value: scanf(“%d”, &stud1.r_no);
scanf(“%s”, &stud1.name;
To input the value: printf(“%d”, &stud1.r_no);
printf(“%s”, &stud1.name);
Size of the structure is the total size of all the member variables. In the above example of structure student
total size is given by,
size=2+1×20+1×20+4=46bytes
A union in C programming is a user defined data type that groups variables of different or same type under
one name but only one variable can store data at the given point in time.
Unions are used to save memory and are useful for applications that involve multiple members, where value
need not be assigned to all the members at any one time.
Syntax:
union union_name
{
data_type var_name1;
data_type var_name2;
-----------------------
};
Union similar to structure except it can store only one-member variable at given point of time therefore the
size of union is the size of largest field of member variable.
Ex:
#include<stdio.h>
struct POINT1
{
int x,y;
};
union POINT2
{
int x:
int y;
};
int main()
{
struct POINT1 p1={2,3};
union POINT2 p2; // union POINT2 p2={4, 5}; is illegal with union.
p2.x=4;
p2.y=5;
printf(“The coordinates of p1 are %d and %d”,p1.x,p1.y);
printf(“The coordinates of p2 are %d and %d”,p2.x,p2.y);
return 0;
}
An enumeration variables are declared same as other variables. They belong to user defined data type called
enumerated data type which consists set of named integer constants known as enumeration constants.
9. List the functions for reading data from file and writing data to files. Explain any one from each.
Functions for reading data from file:
fscanf()
fgets()
fgetc()
fread()
Functions for writing data to files:
fprintf()
fputs()
fputc()
fwrite()
fgets(): the function stands for “file get string”. It is used to get a string from a stream.
Syntax: char *fgets(char *str, int size, FILE *stream);
Reads at most one less than the number of characters specified by size and terminates as soon as it encounters
either a new line character, EOF, or any other error.
#include<stdio.h>
int main()
{
FILE *fp;
char str[80];
fp=fopen(“ABC.DAT”, “r”);
if(fp==NULL)
{
printf(“The file could not be opened”);
exit(1);
}
while(fgets(str, 80, fp)!=NULL) // reads 79 characters during each iteration
printf(“\n %s”, str);
printf(“\n File read. Now closing the file”);
fclose(fp);
return 0;
}
Output: ABCDEFGHIJKL……
File read. Now closing the file
fputs(): the function stands for “file put string”. It is used to write a line to a file.
Syntax: int fputs(const char *str, FILE *stream);
The fputs() function writes the string pointed to by str to the stream pointed to by stream.
On successful, it returns 0, and in case of any error it returns EOF.
#include<stdio.h>
int main()
{
FILE *fp;
char feedback[100];
fp=fopen(“comments.TXT”, “w”)
if(fp==NULL)
{
printf(“The file could not be opened”);
exit(1);
}
printf(“\n provide feedback on this book: ”);
gets(feedback);
fflush(stdin);
fputs(feedback,fp);
fclose(fp);
return 0;
}
Output: provide feedback on this book: good