SlideShare una empresa de Scribd logo
1 de 17
Storage
Classes
 ( Characteristics of Variable )
Meaning of Storage Class
 Each variable declared in C contains not only its
data type but also it has a storage class specified
                      with it.
  If user do not specify the storage class of a
  variable , the compiler will assume its storage

class as default    i.e.   automatic .
Purpose of Storage Class
The storage class of a variable tells us about :-
I.   Storage Place of Variable i.e.   Memory or CPU Registers

II. Initial value of Variable
III. Scope of Variable
IV. Lifetime of Variable i.e. how long variable exists.
How Storage Class Declared ????


              auto int a ,b ;
     Storage Class   Data Type   Variable
Four Type of Storage Class in C
        Language :-
      Automatic Storage Class ( Local Variables )
      Register Storage Class
      Static Storage Class
      External Storage Class
Automatic Storage Class
This is the default storage class for all the variable . It always reinitialize
the value of variable .It is declared as : -

                         auto int a ;
Characteristics                                Meaning
     Storage                                 Memory
  Initial Value             Garbage Value i.e. An Unpredictable Value.
Scope or Visibility    Local or Visible in the Block or function in which it is
                                                 declared.
    Life Time           It retains its value till it is in the block in which it is
                                                 declared.
Use of Automatic Storage Class
  Void main ( )
  {
          auto int i , j = 5 ;
          int k ,m =10 ; // By Default Automatic Storage Class
          printf ( “ value of i = % d n value of j = % d “ , i , j ) ;
          printf ( “ value of k = % d n value of m = % d “ , i , j ) ;
  }
                     Value of i = 2009
                     Value of j = 5                   Garbage Value

                     Value of k = 1005                Variable Value
                     Value of m = 10
Use of Automatic Storage Class
void main( )                        void ck ( )
{                                   {
        void ck() ; //function prototype     int i = 0 ;
        clrscr ( ) ;                         printf ( “nn Value of I ..%d”,i ) ;
        ck ( ) ;                             i++;
        ck ( ) ;                    }
        ck ( ) ;                                  Output
        getch( ) ;                                0
}                                                 0
                                                  0
Register Storage Class
In this storage class , variable is stored in C P U Registers , just for the
sake of increase the execution speed of some variable of program. It is
declares as :-

                              register int a ;

  Characteristics                                Meaning
       Storage                             C P U Registers
    Initial Value                           Garbage Value
 Scope or Visibility           Local to the Block in which it is declared
      Life Time         It retains its value till the control remains in the block
Use of Register Storage Class
    Void main ( )
    {
            register int i ;
            for ( i = 1 ; i < = 100 ; i + + )
            {
                      printf ( “ n % d “ , i ) ;
            }
    }
Use of scanf with register store class variable is invalid
  Prgram give error because register is not associated with any
  memory address.
#include<stdio.h>
int main()
{
register int num; //register variable its scope lies with in the main
//function and can not use with scanf
clrscr();
scanf("%d",&num); // Error is must take address of memory location
getch();
return 0;
}
Static Storage Class
This storage class is used when a user want that a variable should retain its
value even after the execution of the function in which it is declared, then
this storage class is used . It is declared as follow :-

                            static int a ;

  Characteristics                                Meaning
       Storage                                    Memory
    Initial Value                                 Zero ( 0 )
 Scope or Visibility             Local to the block in which it is declared
      Life Time         It retains its value between the different function calls.
Use of Static Storage Class
void main( )            void ck ( )
{                       {
        void ck ( ) ;            static int i = 0 ;
        clrscr ( ) ;             printf ( “nn Value of I ..%d”,i ) ;
        ck ( ) ;                 i++;
        ck ( ) ;        }
        ck ( ) ;                      Output
        getch( ) ;                    0
}                                     1
                                      2
External Storage Class
External variables are declared outside all functions i.e, at the beginning of the
program. Global variables should be available to all the functions with the help
of extern specifier. It is declared as follow : -

                          extern int a ;
  Characteristics                               Meaning
       Storage                                  Memory
    Initial Value                               Zero ( 0 )
 Scope or Visibility               Global ( Visible in all the Program )
      Life Time            It retains its value through out the whole program
Use of External Storage Class
#include<stdio.h>                  void ck ( )
#include<conio.h>                  {
extern int a = 10 ;                         a = a + 10 ;
void ck ( ) ;                               printf ( “nn Value of a ..%d”,a ) ;
void main( )                       }
{         int a = 5 ;
                                                 Output
          printf ( “ %d “ , a) ;
                                                 5
          ck ( ) ;
                                                 20
          getch ( ) ;}
#include<stdio.h>          #include<stdio.h>
#include<conio.h>          #include<conio.h>
int x=21;                  int x=21;
int main()                 int main()
{                          {
int y;                     Extern int y; //external variable declare
 clrscr();                  clrscr();
 printf("%d t %d",x,y);    printf("%d t %d",x,y);
getch();                   getch();
return 0;                  return 0;

}                          }
int y=31;                  int y=31; // extern variable is defined
LHS Program Output is   RHS Program Output is
21 Garbage value        21 31

Más contenido relacionado

La actualidad más candente

Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in CSyed Mustafa
 
Storage Class in C Progrmming
Storage Class in C Progrmming Storage Class in C Progrmming
Storage Class in C Progrmming Kamal Acharya
 
Function in c program
Function in c programFunction in c program
Function in c programumesh patil
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oopsHirra Sultan
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in cPrabhu Govind
 
Classes and objects
Classes and objectsClasses and objects
Classes and objectsNilesh Dalvi
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++LPU
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)Ritika Sharma
 
Templates in C++
Templates in C++Templates in C++
Templates in C++Tech_MX
 
How to execute a C program
How to execute a C  program How to execute a C  program
How to execute a C program Leela Koneru
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
Functions in c++
Functions in c++Functions in c++
Functions in c++Maaz Hasan
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 

La actualidad más candente (20)

Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Storage classes in C
Storage classes in CStorage classes in C
Storage classes in C
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
 
Storage Class in C Progrmming
Storage Class in C Progrmming Storage Class in C Progrmming
Storage Class in C Progrmming
 
Function in c program
Function in c programFunction in c program
Function in c program
 
C functions
C functionsC functions
C functions
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Friend function in c++
Friend function in c++ Friend function in c++
Friend function in c++
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
How to execute a C program
How to execute a C  program How to execute a C  program
How to execute a C program
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 

Destacado

Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and FunctionsJake Bond
 
Storage Class Specifiers in C++
Storage Class Specifiers in C++Storage Class Specifiers in C++
Storage Class Specifiers in C++Reddhi Basu
 
C and its errors
C and its errorsC and its errors
C and its errorsJunaid Raja
 
External & Internal Storage Device ! Batra Computer Centre
External & Internal Storage Device ! Batra Computer CentreExternal & Internal Storage Device ! Batra Computer Centre
External & Internal Storage Device ! Batra Computer Centrejatin batra
 
Basic Data Storage
Basic Data StorageBasic Data Storage
Basic Data Storageneptonia
 
Research approaches - Research Methodology - Manu Melwin Joy
Research approaches - Research Methodology - Manu Melwin JoyResearch approaches - Research Methodology - Manu Melwin Joy
Research approaches - Research Methodology - Manu Melwin Joymanumelwin
 
#OOP_D_ITS - 8th - Class Diagram
#OOP_D_ITS - 8th - Class Diagram#OOP_D_ITS - 8th - Class Diagram
#OOP_D_ITS - 8th - Class DiagramHadziq Fabroyir
 
Basics of storage Technology
Basics of storage TechnologyBasics of storage Technology
Basics of storage TechnologyLopamudra Das
 
Research Methodology
Research MethodologyResearch Methodology
Research Methodologybelooka
 
Five components of communication
Five components of communicationFive components of communication
Five components of communicationjumar dimas
 
Communication skills week 4
Communication skills week 4Communication skills week 4
Communication skills week 4Wardah Azhar
 

Destacado (20)

Storage Classes and Functions
Storage Classes and FunctionsStorage Classes and Functions
Storage Classes and Functions
 
Storage Class Specifiers in C++
Storage Class Specifiers in C++Storage Class Specifiers in C++
Storage Class Specifiers in C++
 
Presented by
Presented byPresented by
Presented by
 
Storage class
Storage classStorage class
Storage class
 
C and its errors
C and its errorsC and its errors
C and its errors
 
External & Internal Storage Device ! Batra Computer Centre
External & Internal Storage Device ! Batra Computer CentreExternal & Internal Storage Device ! Batra Computer Centre
External & Internal Storage Device ! Batra Computer Centre
 
Basic Data Storage
Basic Data StorageBasic Data Storage
Basic Data Storage
 
Static variable
Static variableStatic variable
Static variable
 
Research approaches - Research Methodology - Manu Melwin Joy
Research approaches - Research Methodology - Manu Melwin JoyResearch approaches - Research Methodology - Manu Melwin Joy
Research approaches - Research Methodology - Manu Melwin Joy
 
K to 12 pc hardware servicing learning module
K to 12 pc hardware servicing learning moduleK to 12 pc hardware servicing learning module
K to 12 pc hardware servicing learning module
 
Storage
StorageStorage
Storage
 
#OOP_D_ITS - 8th - Class Diagram
#OOP_D_ITS - 8th - Class Diagram#OOP_D_ITS - 8th - Class Diagram
#OOP_D_ITS - 8th - Class Diagram
 
Basics of storage Technology
Basics of storage TechnologyBasics of storage Technology
Basics of storage Technology
 
Research Methodology
Research MethodologyResearch Methodology
Research Methodology
 
Storage Basics
Storage BasicsStorage Basics
Storage Basics
 
Five components of communication
Five components of communicationFive components of communication
Five components of communication
 
class c++
class c++class c++
class c++
 
Drainage...class 9 cbse
Drainage...class 9 cbseDrainage...class 9 cbse
Drainage...class 9 cbse
 
Communication skills week 4
Communication skills week 4Communication skills week 4
Communication skills week 4
 
Loops in C
Loops in CLoops in C
Loops in C
 

Similar a 11 lec 11 storage class

Similar a 11 lec 11 storage class (20)

Storage class
Storage classStorage class
Storage class
 
Storage classes
Storage classesStorage classes
Storage classes
 
presentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptpresentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.ppt
 
storage class
storage classstorage class
storage class
 
from java to c
from java to cfrom java to c
from java to c
 
Lecture 13 - Storage Classes
Lecture 13 - Storage ClassesLecture 13 - Storage Classes
Lecture 13 - Storage Classes
 
Storage class
Storage classStorage class
Storage class
 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C
 
Storage classes arrays & functions in C Language
Storage classes arrays & functions in C LanguageStorage classes arrays & functions in C Language
Storage classes arrays & functions in C Language
 
Storage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptxStorage_classes_and_Scope_rules.pptx
Storage_classes_and_Scope_rules.pptx
 
What is storage class
What is storage classWhat is storage class
What is storage class
 
Function lecture
Function lectureFunction lecture
Function lecture
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Storage class
Storage classStorage class
Storage class
 
visiblity and scope.pptx
visiblity and scope.pptxvisiblity and scope.pptx
visiblity and scope.pptx
 
C Programming Storage classes, Recursion
C Programming Storage classes, RecursionC Programming Storage classes, Recursion
C Programming Storage classes, Recursion
 
C++ theory
C++ theoryC++ theory
C++ theory
 
Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and Answers
 
DMA.pptx
DMA.pptxDMA.pptx
DMA.pptx
 
Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variables
 

Más de kapil078

12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop kapil078
 
Btech 1st yr midterm 1st
Btech 1st yr midterm 1stBtech 1st yr midterm 1st
Btech 1st yr midterm 1stkapil078
 
Cmp104 lec 7 algorithm and flowcharts
Cmp104 lec 7 algorithm and flowchartsCmp104 lec 7 algorithm and flowcharts
Cmp104 lec 7 algorithm and flowchartskapil078
 
Cmp104 lec 6 computer lang
Cmp104 lec 6 computer langCmp104 lec 6 computer lang
Cmp104 lec 6 computer langkapil078
 
Cmp104 lec 2 number system
Cmp104 lec 2 number systemCmp104 lec 2 number system
Cmp104 lec 2 number systemkapil078
 
Cmp104 lec 6 computer lang
Cmp104 lec 6 computer langCmp104 lec 6 computer lang
Cmp104 lec 6 computer langkapil078
 
Cmp104 lec 4 types of computer
Cmp104 lec 4 types of computerCmp104 lec 4 types of computer
Cmp104 lec 4 types of computerkapil078
 
Cmp104 lec 3 component of computer
Cmp104 lec 3 component of computerCmp104 lec 3 component of computer
Cmp104 lec 3 component of computerkapil078
 
Cmp104 lec 1
Cmp104 lec 1Cmp104 lec 1
Cmp104 lec 1kapil078
 
cmp104 lec 8
cmp104 lec 8cmp104 lec 8
cmp104 lec 8kapil078
 

Más de kapil078 (13)

12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 
Lec 10
Lec 10Lec 10
Lec 10
 
Btech 1st yr midterm 1st
Btech 1st yr midterm 1stBtech 1st yr midterm 1st
Btech 1st yr midterm 1st
 
Lec9
Lec9Lec9
Lec9
 
Cmp 104
Cmp 104Cmp 104
Cmp 104
 
Cmp104 lec 7 algorithm and flowcharts
Cmp104 lec 7 algorithm and flowchartsCmp104 lec 7 algorithm and flowcharts
Cmp104 lec 7 algorithm and flowcharts
 
Cmp104 lec 6 computer lang
Cmp104 lec 6 computer langCmp104 lec 6 computer lang
Cmp104 lec 6 computer lang
 
Cmp104 lec 2 number system
Cmp104 lec 2 number systemCmp104 lec 2 number system
Cmp104 lec 2 number system
 
Cmp104 lec 6 computer lang
Cmp104 lec 6 computer langCmp104 lec 6 computer lang
Cmp104 lec 6 computer lang
 
Cmp104 lec 4 types of computer
Cmp104 lec 4 types of computerCmp104 lec 4 types of computer
Cmp104 lec 4 types of computer
 
Cmp104 lec 3 component of computer
Cmp104 lec 3 component of computerCmp104 lec 3 component of computer
Cmp104 lec 3 component of computer
 
Cmp104 lec 1
Cmp104 lec 1Cmp104 lec 1
Cmp104 lec 1
 
cmp104 lec 8
cmp104 lec 8cmp104 lec 8
cmp104 lec 8
 

11 lec 11 storage class

  • 2. Meaning of Storage Class Each variable declared in C contains not only its data type but also it has a storage class specified with it. If user do not specify the storage class of a variable , the compiler will assume its storage class as default i.e. automatic .
  • 3. Purpose of Storage Class The storage class of a variable tells us about :- I. Storage Place of Variable i.e. Memory or CPU Registers II. Initial value of Variable III. Scope of Variable IV. Lifetime of Variable i.e. how long variable exists.
  • 4. How Storage Class Declared ???? auto int a ,b ; Storage Class Data Type Variable
  • 5. Four Type of Storage Class in C Language :-  Automatic Storage Class ( Local Variables )  Register Storage Class  Static Storage Class  External Storage Class
  • 6. Automatic Storage Class This is the default storage class for all the variable . It always reinitialize the value of variable .It is declared as : - auto int a ; Characteristics Meaning Storage Memory Initial Value Garbage Value i.e. An Unpredictable Value. Scope or Visibility Local or Visible in the Block or function in which it is declared. Life Time It retains its value till it is in the block in which it is declared.
  • 7. Use of Automatic Storage Class Void main ( ) { auto int i , j = 5 ; int k ,m =10 ; // By Default Automatic Storage Class printf ( “ value of i = % d n value of j = % d “ , i , j ) ; printf ( “ value of k = % d n value of m = % d “ , i , j ) ; } Value of i = 2009 Value of j = 5 Garbage Value Value of k = 1005 Variable Value Value of m = 10
  • 8. Use of Automatic Storage Class void main( ) void ck ( ) { { void ck() ; //function prototype int i = 0 ; clrscr ( ) ; printf ( “nn Value of I ..%d”,i ) ; ck ( ) ; i++; ck ( ) ; } ck ( ) ; Output getch( ) ; 0 } 0 0
  • 9. Register Storage Class In this storage class , variable is stored in C P U Registers , just for the sake of increase the execution speed of some variable of program. It is declares as :- register int a ; Characteristics Meaning Storage C P U Registers Initial Value Garbage Value Scope or Visibility Local to the Block in which it is declared Life Time It retains its value till the control remains in the block
  • 10. Use of Register Storage Class Void main ( ) { register int i ; for ( i = 1 ; i < = 100 ; i + + ) { printf ( “ n % d “ , i ) ; } }
  • 11. Use of scanf with register store class variable is invalid Prgram give error because register is not associated with any memory address. #include<stdio.h> int main() { register int num; //register variable its scope lies with in the main //function and can not use with scanf clrscr(); scanf("%d",&num); // Error is must take address of memory location getch(); return 0; }
  • 12. Static Storage Class This storage class is used when a user want that a variable should retain its value even after the execution of the function in which it is declared, then this storage class is used . It is declared as follow :- static int a ; Characteristics Meaning Storage Memory Initial Value Zero ( 0 ) Scope or Visibility Local to the block in which it is declared Life Time It retains its value between the different function calls.
  • 13. Use of Static Storage Class void main( ) void ck ( ) { { void ck ( ) ; static int i = 0 ; clrscr ( ) ; printf ( “nn Value of I ..%d”,i ) ; ck ( ) ; i++; ck ( ) ; } ck ( ) ; Output getch( ) ; 0 } 1 2
  • 14. External Storage Class External variables are declared outside all functions i.e, at the beginning of the program. Global variables should be available to all the functions with the help of extern specifier. It is declared as follow : - extern int a ; Characteristics Meaning Storage Memory Initial Value Zero ( 0 ) Scope or Visibility Global ( Visible in all the Program ) Life Time It retains its value through out the whole program
  • 15. Use of External Storage Class #include<stdio.h> void ck ( ) #include<conio.h> { extern int a = 10 ; a = a + 10 ; void ck ( ) ; printf ( “nn Value of a ..%d”,a ) ; void main( ) } { int a = 5 ; Output printf ( “ %d “ , a) ; 5 ck ( ) ; 20 getch ( ) ;}
  • 16. #include<stdio.h> #include<stdio.h> #include<conio.h> #include<conio.h> int x=21; int x=21; int main() int main() { { int y; Extern int y; //external variable declare clrscr(); clrscr(); printf("%d t %d",x,y); printf("%d t %d",x,y); getch(); getch(); return 0; return 0; } } int y=31; int y=31; // extern variable is defined
  • 17. LHS Program Output is RHS Program Output is 21 Garbage value 21 31

Notas del editor

  1. #include&lt;stdio.h&gt;#include&lt;conio.h&gt;int gnum=10; //global variable declare outside the main function its scope is limited to whole program existint main(){void print(); //function prototype because it has return type void and semicolonint num=5; //local variable its scope lies with in the main functionclrscr();printf(&quot;in main local variable num is %d \\n in main global variable gnum is %d&quot;,num,gnum);print(); //function calling getch();return 0;}void print () //functiion definaton{printf(&quot;\\n Global variable is %d&quot;,gnum);printf(&quot;main variable num%d&quot;,num); // program gives error at this point and said undefined symbol num because num scope is limited only to the main function.to run the program make the comment this line}
  2. Storage 2.c