0% found this document useful (0 votes)
34 views

Assignment No 04

This document contains the answers to various questions related to storage classes in C programming. [1] It explains the four storage classes in C - automatic, register, static, and external and provides examples to demonstrate each. [2] It discusses when each storage class should be used - automatic for block scopes, register for frequently used variables, static for recursive functions, and external for global variables shared across files. [3] It provides the output for a sample program using static storage class in recursive function calls.

Uploaded by

Shweta Singh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Assignment No 04

This document contains the answers to various questions related to storage classes in C programming. [1] It explains the four storage classes in C - automatic, register, static, and external and provides examples to demonstrate each. [2] It discusses when each storage class should be used - automatic for block scopes, register for frequently used variables, static for recursive functions, and external for global variables shared across files. [3] It provides the output for a sample program using static storage class in recursive function calls.

Uploaded by

Shweta Singh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

ASSIGNMENT NO.

:- 04
NAME:- SHWETA SINGH

ROLL.NO.:- B 54

REG.NO.:- 11009865

PART:- A
1. Explain the various storage classes in C ?

ANS:- There are four storage classes in c:-

a) Automatic storage class


b) Register storage class
c) Static storage class
d) External storage class

1. Automatic storage class:- automatic storage class is that storage


class in which if the variable is not initialized it contains a
garbage value.
The features of a variable defined to have an automatic
storage class are as under:-
1. Storage:- memory
2. Default initial value:- an unpredictable value, which is often
called a garbage value.
3.Scope:- local to the block in which the variable is defined.
4.Life:- Till the control remains within the block in which the
variable is defined.

Following program shows how an automatic storage class


variable is declared:-
# 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

2. REGISTER STORAGE CLASS:- The features of a variable


defined to be of register storage class are as under:-
a) Storage :- cpu registers.
b) Default initial value:- garbage value
c) Scope:- local to the block in which the variable is defined.
d) Life:- till the control remains within the block in which the
variable is defined.

Program for register storage class:-


#include<stdio.h>
#include<conio.h>
int main()
{
Register int i;
For( i=1;i<=10;i++)
Printf(“%d\n”,i);
Return 0;
Getch();
}

3. STATIC STORAGE CLASS:- The features of a variable


defined to have a static storage class are as under:-
a) Storage:- memory
b) Default initial value:- zero
c) Scope:- local to the block in which the variable is defined.
d) Life:- value of the variable persists between different
function calls.

PROGRAM FOR STATIC STORAGE CLASS:-

#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;

OUTPUT OF THE ABOVE PROGRAM WOULD BE:-

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.

2. Among all the storage classes which is to use and when?

Ans:- there are 4 storage classes in c:


1. Automatic storage class
2. Register storage class
3. Static storage class
4. External storage class
Where we use storage classes and when:-

Static storage class :


. On the other hand, the static storage class specifier can be used
in a function declaration only if it is at file scope.- In the case of
recursive function calls we use static  storage class.
Example:-
#include<stdio.h>
#include<conio.h>
void main()
{
int I;
clrscr();
for(i=1; i<=3; i++)
stat();
{
static int x=0;
x=x+1;
printf(“x=%d\n”,x);
}
getch();
}

 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

Register storage class:- Used in the case of frequently used variables.


Example:-
#include<stdio.h>
#include<conio.h>
int main()
{
Register int i;
For( i=1;i<=10;i++)
Printf(“%d\n”,i);
Return 0;
Getch();
}

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

Q3. What will be the output :-


main()
{
int i,j;
for(i=1;i<5;i++)
{
j=g(i);
printf(“\n %d”,j);
}
}
g(int x)
{
static int v=1;
int b=3;
v+=x;
return(v+x+b);
}

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);

printf("contents of the file are\n");

fp1=fopen("first.txt","r");

if(fp1==NULL)

printf("file does not exist");


fp2=fopen("second.txt","w");

while(!feof(fp1))

c=fgetc(fp1);

fputc(c,fp2)

fclose(fp1);

printf("new file after copying");

fp2=fopen("second.txt","r");

while(!feof(fp2))

c=fgetc(fp2);

printf("%c",c);

fclose(fp2);

getch();

You might also like