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++
- Auto
- Register
- Static
- Extern
- Auto Storage Class:- This is the default storage class for local variables.
- Register Storage Class:-This is used to define the local variable that should be stored in register instead of RAM.
- Static Storage Class:-This is the default storage class for global variables.
- Extern Storage Class:- Extern is use to give a reference of global variable that is visible to all the program files.
{ int num; auto int month; }
{ register int miles; }
{ int num; static int count; main() cout<<count; }
//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 |