Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Storage classes in C++

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 49
    Comment on it

    A storage class defines the scope (visibility) and the life time of the variable in the program or in the class.

    There are following storage classes:

    • auto

    • register

    • static

    • extern

     

    The auto Storage Class

    It is the default storage class for all the variables.

    {
       int mount;
       auto int month;
    }

     

    The register Storage Class

    The register storage class store the variable into the registers not in the RAM.

    You cannot apply unary operator into it.

     

    {
       register int  miles;
    }

     

     

    The static Storage Class

    The static storage class instructs the compiler to keep the variable into the memory until program executes.

    #include <iostream>
     
    // Function declaration
    void func(void);
     
    static int count = 10; /* Global variable */
     
    main()
    {
        while(count--)
        {
           func();
        }
        return 0;
    }
    // Function definition
    void func( void )
    {
        static int i = 5; // local static variable
        i++;
        std::cout << "i is " << i ;
        std::cout << " and count is " << count << std::endl;
    }

     

    The extern Storage Class

    The extern storage class is used to defined a reference of a global variable accessible by every class.

     

    First Program
    
    #include <iostream>
     
    int count ;
    extern void write_extern();
     
    main()
    {
       count = 5;
       write_extern();
    }
    
    
    Second Program
    
    
    #include <iostream>
     
    extern int count;
     
    void write_extern(void)
    {
       std::cout << "Count is " << count << std::endl;
    }

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: