Storage classes

431 views
365 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
431
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Storage classes

  1. 1. Storage Classes
  2. 2. auto - storage class <ul><li>Automatic variables may be specified upon declaration </li></ul><ul><li>to be of storage class auto. However, it is not required; by </li></ul><ul><li>default, storage class within a block is auto. </li></ul><ul><li>Automatic variables declared with initializes are initialized each </li></ul><ul><li>time the block in which they are declared is entered. </li></ul><ul><li>Memory is allocated automatically upon entry to a block and </li></ul><ul><li>freed automatically upon exit from the block </li></ul><ul><li>The scope of automatic variables is local to the block in which </li></ul><ul><li>they are declared, including any blocks nested within that block. </li></ul>
  3. 3. <ul><li>void function(void); </li></ul><ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li>int i=0; </li></ul><ul><li>while(i<4) </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot;i = %d &quot;,i); </li></ul><ul><li>function(); i++; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>void function(void) </li></ul><ul><li>{ </li></ul><ul><li>int i = 4; </li></ul><ul><li>printf(&quot;Initial Value of i = %d &quot;,i); </li></ul><ul><li>i = 7; </li></ul><ul><li>printf(&quot; Final Value of i = %d &quot;,i); </li></ul><ul><li>} </li></ul>i = 0 Initial Value of i = 4 Final Value of i = 7 i = 1 Initial Value of i = 4 Final Value of i = 7 i = 2 Initial Value of i = 4 Final Value of i = 7 i = 3 Initial Value of i = 4 Final value of i = 7
  4. 4. <ul><li>{ </li></ul><ul><li>int Count; </li></ul><ul><li>auto int Month; </li></ul><ul><li>} </li></ul><ul><li>auto int Month is the same as int Count </li></ul><ul><li>because it is the default, it is almost never used </li></ul>
  5. 5. Register Variables <ul><li>Register variables are a special case of automatic </li></ul><ul><li>variables. Automatic variables are allocated storage in the </li></ul><ul><li>memory of the computer; however, for most computers, </li></ul><ul><li>accessing data in memory is considerably slower than </li></ul><ul><li>processing in the CPU. These computers often have small </li></ul><ul><li>amounts of storage within the CPU itself where data can be </li></ul><ul><li>stored and accessed quickly. These storage cells are </li></ul><ul><li>called registers . </li></ul>
  6. 6. <ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>/* block scope with the register specifier */ </li></ul><ul><li>register int i; </li></ul><ul><li>. . . </li></ul><ul><li>for (i=0; i<MAX_NUM; i++) </li></ul><ul><li>{ /* some statements */ } </li></ul><ul><li>. . . </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
  7. 7. Declaration vs Definition <ul><li>Declaration is simply to describe information </li></ul><ul><li>``about'' the variable. </li></ul><ul><li>Latter action of the compiler, allocation of </li></ul><ul><li>storage, is more properly called the </li></ul><ul><li>definition of the variable. </li></ul>
  8. 8. External Variables <ul><li>External variables may be declared outside </li></ul><ul><li>any function block in a source code file the same </li></ul><ul><li>way any other variable is declared; by specifying </li></ul><ul><li>its type and name. No storage class specifier is </li></ul><ul><li>used - the position of the declaration within the file </li></ul><ul><li>indicates external storage class. Memory for such </li></ul><ul><li>variables is allocated when the program begins </li></ul><ul><li>execution, and remains allocated until the program </li></ul><ul><li>terminates. </li></ul>
  9. 9. <ul><li>#include<stdio.h> </li></ul><ul><li>int a=1; </li></ul><ul><li>void next(void); </li></ul><ul><li>void next1(void); </li></ul><ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li>printf(&quot;Declalation vs Definition &quot;); </li></ul><ul><li>a=2; </li></ul><ul><li>printf(&quot;a=%d &quot;,a); </li></ul><ul><li>next(); </li></ul><ul><li>next1(); </li></ul><ul><li>printf(&quot;a=%d &quot;,a); </li></ul><ul><li>} </li></ul><ul><li>int b=0; /* definition of external b*/ </li></ul><ul><li>void next(void) </li></ul><ul><li>{ </li></ul><ul><li>char a; /* auto a is defined*/ </li></ul><ul><li>a='a'; </li></ul><ul><li>b=77; /* external b accessed */ </li></ul><ul><li>} </li></ul><ul><li>extern int a; </li></ul><ul><li>void next1(void) </li></ul><ul><li>{ </li></ul><ul><li>float b; /* auto b defined */ </li></ul><ul><li>b=19.2; </li></ul><ul><li>a=13; /* external a is accesed*/ </li></ul><ul><li>} </li></ul>
  10. 10. Static Variables <ul><li>Static automatic variables continue to exist </li></ul><ul><li>even after the block in which they are </li></ul><ul><li>defined terminates. Thus, the value of a </li></ul><ul><li>static variable in a function is retained </li></ul><ul><li>between repeated function calls to the same </li></ul><ul><li>function. </li></ul>
  11. 11. <ul><li>#include<stdio.h> </li></ul><ul><li>#define MAX 20 </li></ul><ul><li>void sumit(); </li></ul><ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li>int count; </li></ul><ul><li>printf(&quot;Enter 5 Numbers &quot;); </li></ul><ul><li>for(count=0;count<5;count++) </li></ul><ul><li>{ </li></ul><ul><li>sumit(); </li></ul><ul><li>} </li></ul><ul><li>printf(&quot;Program Completed&quot;); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul><ul><li>void sumit() </li></ul><ul><li>{ </li></ul><ul><li>static int sum=0; </li></ul><ul><li>int num; </li></ul><ul><li>printf(&quot;Enter a Number &quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&num); </li></ul><ul><li>sum=sum+num; </li></ul><ul><li>printf(&quot;The Current Total is:%d &quot;,sum); </li></ul><ul><li>} </li></ul>

×