Data Type C++

adplus-dvertising
C++ Storage Classes
Previous Home Next

Storage classes define the scope and lifetime of the variable and function within a C program.

Types of Storage Classes in C++

  1. Auto
  2. Register
  3. Static
  4. Extern
  1. Auto Storage Class:- This is the default storage class for local variables.
  2. For example:
     {
    int num;
    auto int month; 
     }
    
  3. Register Storage Class:-This is used to define the local variable that should be stored in register instead of RAM.
  4. For example.
     {
     register int miles; 
     }
    
  5. Static Storage Class:-This is the default storage class for global variables.
  6. For example:
    {
    int num;
    static int count;
    main()
    cout<<count;
    }
    
  7. Extern Storage Class:- Extern is use to give a reference of global variable that is visible to all the program files.
  8. For example:
    //File 1: main.cpp
    #include <iostream>
    #include <conio.h>
    int count ;
    extern void write_extern();
    main()
    {
    count = 5;
    write_extern();
    } 
    
    //File 2: write.cpp
    #include <iostream>
    #include <conio.h>
    extern int count;
    void write_extern(void)
    {
    std::cout << "Count is " << count << std::endl;
    } 
    

    Note:-Here extern keyword is being used to declare count in another file.

Previous Home Next