Storage class in C / C ++

The Storage Class defines the scope and life of variables and / or functions within a C / C ++ program. They often precede the type of data they impact.

The Storage Class defines the scope and life of variables and / or functions within a C / C ++ program. They often precede the type of data they impact. Here are the storage classes that can be used in C / C ++:

auto

register

static

extern

mutable

Storage class is auto in C / C ++

The storage class auto in C / C ++ is the default storage class for all local variables in C / C ++:

 { int diemthi ; auto int diemthi ; } 

The example above defines two variables with the same storage class, auto can only be used inside welding, for example, for internal variables.

The class stores the register in C / C ++

The register class in C / C ++ is used to define local variables that should be stored in a register instead of RAM. That is, the variable has a maximum size equal to the register size (usually 1 word) and cannot have a single operator '&' applied to it (because there is no memory address).

 { register int hocphi ; } 

The register class should only be used for variables that require quick access such as counters. Note also that a variable defined with 'register' does not mean that the variable is stored in the register. That is, it can be stored in the register depending on the hardware and the execution limit.

Static storage class in C / C ++

The static storage class in C / C ++ tells the compiler to keep a local variable that exists for the entire lifetime of the program instead of creating and destroying the variable each time it enters and exits the variable scope. Therefore, variables have static that allows it to maintain values ​​between function calls.

Static storage classes can also be applied to global variables (global). When applied to global variables, it tells the compiler that the scope of the global variable is limited to the file in which it is declared.

In C / C ++, when static is used on the data member of the class, it causes: only one copy of that member is shared by all objects in its class.

 #include // phan khai bao ham void func ( void ); static int biendem = 10 ; /* Day la bien toan cuc */ main () { while ( biendem --) { func (); } return 0 ; } // Phan dinh nghia ham void func ( void ) { static int i = 5 ; // Day la bien cuc bo dang static i ++; std :: cout << "i co gia tri la " << i ; std :: cout << " va biendem co gia tri la " << biendem << std :: endl ; } 

Running the above C / C ++ program will produce the following results:

Storage class in C / C ++ Picture 1Storage class in C / C ++ Picture 1

The class stores extern in C / C ++

The extern class stored in C / C ++ is used to provide a reference of a global variable seen by ALL program files. When you use 'extern', the variable cannot be initialized, when it points to the variable name at a storage class location that was previously defined.

When you have multiple files and you define a global variable or function in a file and also want to use it in other files, extern is used in another file to provide the reference of the defined variable or function. Remember, extern is used to declare a global variable or function in another file.

The class that stores extern is commonly used when there are two or more files that share the same variable or global function. See example with the following two files:

First file: extern1.cpp

 #include int biendem ; extern void vidu_extern (); main () { biendem = 5 ; vidu_extern (); } 

The second file: extern2.cpp

 #include extern int biendem ; void vidu_extern ( void ) { std :: cout << "Gia tri biendem la " << count << std :: endl ; } 

Here, the keyword extern is being used to declare biendem in another file. Now compile these two files as follows:

 $g ++ extern1 . cpp extern2 . cpp - o write 

It will create the executable write program, try to write and test the following result:

 $ ./ write 5 

Mutable storage class in C / C ++

Mutable storage classes in C / C ++ only apply to class objects, which will be discussed in later chapters. It allows a member of an object to override. That is, a member that is mutable can be modified by a const member function.

According to Tutorialspoint

Previous post: Modifier in C / C ++

Next lesson: Operator in C ++

4 ★ | 2 Vote