Assignment No 04
Assignment No 04
:- 04
NAME:- SHWETA SINGH
ROLL.NO.:- B 54
REG.NO.:- 11009865
PART:- A
1. Explain the various storage classes in C ?
#include<stdio.h>
#include<conio.h>
Void increment();
Int main()
{ increment();
Increment();
Increment();
Return 0;
Void increment()
Static int i= 1;
Printf(“%d\n”,i);
i=i+1;
1 2 3
4. EXTERNAL STORAGE CLASS:- The features of a variable
whose storage class has been defined as external are as
follows:- storage:- memory
Default initial value:- zero.
Scope:- global
Life:- as long as the program’s execution doesn’t come to an
end.
Auto storage class:- The auto storage-class specifiers can be used only to
declare names used in blocks or to declare formal arguments to functions.
Example:- # include<stdio.h>
# include< conio.h>
Void main()
{
Auto int i,j;
Printf( “%d %d\n”,i,j);
Return 0;
Getch();
}
OUTPUT OF THE GIVEN PROGRAM COULD BE:- 1211
221
Extern storage class:- For only those variables are used almost all
functions in the program. This would avoid unnecessary passing of the
variables. This is always written outside of main program, and it works
throughout the program.
Example:-
#include <stdio.h>
#include <conio.h>
extern int i=15;
void main()
{
int i=45;
void show(void);
clrscr();
printf("%d",i);
show();
getch();
}
void show(void)
{
printf("%d",i);
}
Output:-
45
15
Output:-
6
9
13
18
Explanation:-
The initial value of x is 1.
V=1
And b=1
Since v=v+x=1+1=2
V+x+b=2+1+3=6
Similarly
V=v+x=2+2=4
V+x+b=4+2+3=9
Again
V=v+x=4+3=7
V+x+b=7+3+3=13
Again
V=v+x=7+4=11
V+x+b=11+4+3=18
PART-B
Q4. Is it necessary that a file created in text mode must always be opened in text
mode for subsequent operations? What is the appropriate answer? Explain
with example. A program to demonstrate it
Ans:-
Yes,it is necessary that a file created in text mode must always be opened
in text mode for subsequent operations.
Because when open a file in library mode it gives us file in graphic which
is not easily understandable by user. If the file is not opened in the text
mode we will not be able to write in the file.
#include<stdio.h>
#include<conio.h>
Void main()
{
FILE *fp;
Fp=fopen(“first.txt”,”w”);
While(c!=’.’)
{
C=getche();
Fputc(c,fp)\
}
Fpclose(fp);
Getch();
}
Now we have written the file in the necessary syntax
And now we need to open it and for that
#include<stdio.h>
#include<conio.h>
Void main()
{
FILE *fp;
Fp=fopen(“first.txt”,”a”);
While(c!=’.’)
{
C=getche();
Fputc(c,fp);
}
Fpclose(fp);
Getch();
Q5. WAP that prompts the user to input name of a text file, read the text file &
output number of vowels and words in the text.
Ans:-
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
int y,v=0,w=0,o=0,e=0,l=0,d=0,s=0,f=0,g=0;
char c;
fp=fopen("first.txt","w");
if(fp==NULL)
{
printf("\n file can't be open");
}
printf("\n write contest in the file & press'.' to stop \n");
while(c!='.')
{
c=getche();
fputc(c,fp);
}
fclose(fp);
printf("\n file contents details are...............\n");
printf("*******************************\n");
fp=fopen("first.txt","r");
while((y=fgetc(fp))!=EOF)
{
switch(y)
{
case 'a':
v++;
break;
case 'e':
o++;
break;
case 'i':
w++;
break;
case 'o':
e++;
break;
case 'u':
l++;
break;
case '.':
f++;
break;
case ' ':
g++;
break;
default:
d++;
}
}
printf("\n number of vowels is = %d ",v+o+w+e+l);
printf("\n total number of words = %d",v+o+w+e+l+d);
getch();
}
Q6. Write a program to copy the contents of one FILE into another FILE.
Ans:- #include<stdio.h>
#include<conio.h>
void main()
char c;
FILE*fp1,*fp2;
fp1=fopen("first.txt","w");
while(c!='.')
c=getche();
fputc(c,fp1);
fclose(fp1);
fp1=fopen("first.txt","r");
if(fp1==NULL)
while(!feof(fp1))
c=fgetc(fp1);
fputc(c,fp2)
fclose(fp1);
fp2=fopen("second.txt","r");
while(!feof(fp2))
c=fgetc(fp2);
printf("%c",c);
fclose(fp2);
getch();