There are four different storage class specifications in C Language - automatic , external , static and register.They are identified by the keywords : auto , extern , static and register.
The storage class associated with a variable can sometimes be established simply by the location of the declaration of the variable within the program. In other situations however the keyword has to be placed before the variable name.
Few declarations of variables with their storage class.
auto int a,b,c;
extern float x,y;
static int count;
here variables a,b and c are declared as automatic integer variables while the variables x and y are declared as external floating point variable and the last variable count is a static integer variable.
Automatic storage class
Variables declared within a function body are always Automatic variables unless a different storage class specification is used at the time of declaration of the variable. The scope or lifetime of such variables is confined within the body of the function. Automatic variables declared in different functions of the same program are therefore independent of one another and they may have the same name.
Normally we don’t need to specify the keyword auto before the variable name at the time of declaration but if we use there is no harm.
#include<stdio.h>
long factorial(int n);
void main()
{
auto int n;
clrscr();
printf("Enter the number :-");
scanf("%d",&n);
printf("The factorial value is:-%ld",factorial(n));
getch();
}
long factorial(int n)
{
auto long fact=1;
while(n>1)
{
fact=fact*n;
n=n-1;
}
return fact;
}
Here in the above program we have used automatic variables and that was explicitly declared. An automatic variable does not retain its value once control is transferred out of its defining function.
Here is another program, which will take multiple lines of text, and it will return the average number of characters in each line.
#include<stdio.h>
int linecount();
void main()
{
int n,sum,count;
float avg;
clrscr();
sum=0;
count=0;
printf("Enter the text ( multiple line ):-");
while((n=linecount())>0)
{
sum+=n;
count++;
}
avg=(float)sum/count;
printf("Average number of characters per line :-%5.2f",avg);
getch();
}
int linecount()
{
char line[80];
int count=0;
while((line[count]=getchar())!='\n')
{
count++;
}
return count;
}
External storage class
External variables are not confined to a single function like automatic variables. Their scope extends from the point of definition through the remainder of the program. They are normally referred to as global variables.
An external variable declaration is written in the same manner as an ordinary variable declaration. It must appear outside of and usually before the functions that access the external variable. The keyword extern is not required in external variable declaration as this variables will be specified by the location of their declaration within the program. Any alteration to the value of an external variable within a function will be recognized within the entire scope of the external variable.
#include<stdio.h>
int count; //external variable
void charCount(char ch[]);
void display();
void main()
{
char str[100];
clrscr();
count=0;
puts("Enter the text :-");
gets(str);
charCount(str);
display();
getch();
}
void charCount(char ch[])
{
int i;
i=0;
while(ch[i]!='\0')
{
count++;
i++;
}
}
void display()
{
printf("The total number of characters are :-%d",count);
}
Use of external variable .Take ten numbers from the user and display them in sorted order.
#include <stdio.h>
void getnum();
void printnum();
void sortnum();
int numbers[10];
void main()
{
clrscr();
printf("Program for sorting.\n\n");
getnum();
printf("The original array\n");
printnum();
sortnum();
printf("The sorted array\n");
printnum();
getch();
}
void getnum()
{
int counter;
for (counter = 0; counter < 10; counter++)
{
printf("Enter any integer %-2d : ", counter+1);
scanf("%d", &numbers[counter]);
}
printf("End of input\n\n");
}
void printnum()
{
int counter;
for (counter = 0; counter < 10; counter++)
{
printf("%d ", numbers[counter]);
}
printf("\n\n");
}
void sortnum()
{
int counter,flag,temp;
flag = 0;
while (flag == 0)
{
flag = 1;
for (counter = 0; counter < 9; counter++)
{
if (numbers[counter] < numbers[counter+1])
{
temp = numbers[counter];
numbers[counter] = numbers[counter+1];
numbers[counter+1] = temp;
flag = 0;
}
}
}
}
No comments:
Post a Comment