Storage classes
Upcoming SlideShare
Loading in...5
×
 

Storage classes

on

  • 417 views

 

Statistics

Views

Total Views
417
Views on SlideShare
417
Embed Views
0

Actions

Likes
0
Downloads
7
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Storage classes Storage classes Presentation Transcript

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