Sunday, 3 April 2016

Storage Class C / C++ Brush up

C language

Storage Class
storage classes tell you about for things

  1. initial value
  2. meomory location
  3. scope of variable
  4. lifetime of variable



1)auto -meomory create at run time

-declare inside main( )
-in variable default is auto 
  1. //even if u don't write auto then also program works same
  2. #include<stdio.h>
  3. #include<conio.h>
  4.  
  5. int main()
  6. {
  7.     double goa;
  8.     auto int i;          //here i don't have any value therefore it will print some garbage value 
  9.     printf("i=%d",i);
  10. goa=addi();
  11.  printf("goa i=%d",goa);
  12. getch();
  13. return 0;
  14. }


  15. int addi()     /
  16. {
  17.     auto int i ;   
  18.     printf("add i=%d",i);  //now here i value will change as this is auto and scope of that i ended
  19.     return i;
  20. }
  21. /*u know how scope overs ? i tell u this code works in stack area of ram and when scope overs
  22. then in stack meomory previous i value get poped i.e it get deleted.
  23. and Now as soon as i get initialised in addi() at that time lifetime of previous i variable get 
  24. over */
summary

  1. initial value-garbage value
  2. meomory location-stack (in every case meomory created in stack at compile time)
  3. scope of variable-Local
  4. lifetime of variable-Till control remain with that function
2)register -meomory create at compile time

-store in register and not in ram 
-for very fast access suppose you have make counter
  1. #include <stdio.h>
  2. #include <conio.h>
  3. //register storage class ..very fast storage class use in cpu

  4. int main()
  5.    {
  6.           
  7.    reg int i;   
  8.    
  9.    if(i--)
  10.    {
  11.           printf("%d",i);
  12.           main();
  13. }
  14.    
  15.    
  16.    
  17.    getch();
  18.    return 0;
  19.           }
summary
-same summary as auto the only difference here accessing is through cpu rather than ram due to which it is a quick access


  • initial value-garbage value
  • memory location-stack
  • scope of variable-Local
  • lifetime of variable-Till control remain with that function
  • 3)static-meomory create at runtime

    1. #include <stdio.h>
    2. #include <conio.h>

    3.                                                               //no i outside
    4. int main()
    5.    {
    6.           
    7.    static int i=3;   
    8.    
    9.    if(i--)
    10.    {
    11.           printf("%d",i);
    12.           main();
    13. }
    14.    
    15.    
    16.    
    17.    getch();
    18.    return 0;
    19.           }
    in above program your output will be 210 but now in above program if u don't write keyword static i.e if u write code

    1. #include <stdio.h>
    2. #include <conio.h>

    3.                                                               
    4. int main()
    5.    {
    6.           
    7.    int i=3;   
    8.    
    9.    if(i--)
    10.    {
    11.           printf("%d",i);
    12.           main();
    13. }
    14.    
    15.    
    16.    
    17.    getch();
    18.    return 0;
    19.           }


    then you will get 22222222........infinite times as in that case there will be just int which is auto storage class as we know . so, in this case meomory will be created at runtime .
    IN COMPILER PROGRAM FIRST COMPILED THEN COMES ON RUNTIME

    SO U CAN SAY STATIC NOT SUPPORT RECURSION
    summary

    1. initial value-0 and not garbage value
    2. memory location-stack 
    3. scope of variable-Local
    4. lifetime of variable-Till control remain with that function
    4)extern-meomory create at runtime

    With this storage class you can access same common variable in any function all across program
    1. #include <stdio.h>
    2. #include <conio.h>

    3. int i=3;                                                                  //i declare outside here
    4. int main()
    5.    {
    6.           
    7.    extern int i;   //this statement means don't create seprate meom for i here as i is already 
    8.                          //created globally
    9. //let if u write here i=5 then it will change i value all across program.   
    10.    if(i--)
    11.    {
    12.           printf("%d",i);
    13.           main();
    14. }
    15.    
    16.    
    17.    
    18.    getch();
    19.    return 0;
    20.           }
    HERE IN CASE IF U DON'T INITIALIZE YOUR VARIABLE GLOBALLY THEN U WILL GET AN ERROR.


    summary


  • initial value-garbage value
  • memory location-stack
  • scope of variable-Local
  • lifetime of variable-Till control remain with that function
  • No comments:

    Post a Comment