My Profile
Active Members
TodayLast 7 Days
more...
Awards & Gifts
Online Exams
Fresher Jobs
Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian
cities including Bangalore, Chennai, Hyderabad, Pune or Kochi
Resources
Find educational articles, blogs, discussion threads and other resources.
Colleges
Find details about any college in India or search for courses.
|
Resources » Articles/Knowledge Sharing » Computer & Technology »
Storage class in C Language
|
There are two different ways to characterize variables : by data type and by storage class. Data type refers to the type of information the represented by the variable while storage class refers to the permanence of a variable and it’s scope within a program I.e. the portion of the program over which the variable is recognized.
There are four different storage class specifications in C : 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 Variable : 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 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 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 variables : 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 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 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; } } } }
Static variables Occasionally it is desirable to declare a variable within a function whose value will be stored throughout the execution of the program. If we declare a variable within a function ( auto ) then the variable is allocated and deallocated each time the function is invoked. But if we declare a static variable within a function then when the function is exited , the variable retains its value. #include void count(int , int); void main() { int num1,num2; clrscr(); printf("Enter two numbers:-"); scanf("%d%d",&num1,&num2); count(num1,num2); printf("Another two numbers:-"); scanf("%d%d",&num1,&num2); count(num1,num2); printf("%d",c); /*If we try to access ‘c’ here compiler will show error. because ‘c’ is defined as a local static variable.*/ getch(); } void count(int a,int b) { static int c; a=a+b; c++; printf("Sum is :-%d",a); printf("\nYou have send %d pair of values.",c); } use of static variable in function #include int Static( ); int Dynamic( ); static int count = 0; void main() { int i; clrscr(); printf("\nProgram for use of static variable in function\n"); printf("count is %d\n",count); for (i=0; i<9; i++) { count ++; printf("count = %d, static count = %d, dynamic count = %d\n",count, Static(), Dynamic()); } getch(); } int Static( ) { static int i = 0; /* declare and initialize the static variable */ return( ++i ); } int Dynamic( ) { int i = 0; /* declare and initialize the dynamic variable */ return( ++i ); }
Register variable
Registers are special storage areas within a computers CPU.The actual Arithmetic and Logical operations are done within Registers and these operations are carried out by transferring datas from computers memory to registers , the indicated operations are carried out and then datas are transferred to memory. This process is executed several times in a program execution. For programs the execution time can be reduced if we store certain values within register rather than memory. In C a variable can be assigned this storage class simply by preceding the type declaration with the keyword register. Register variables are closely related to auto variables , except address operator ( & ) cannot be applied on them.
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|
Advertise Here
|