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. 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:
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 ++
You should read it
May be interested
- Operator in C ++an operator is an icon, which tells the compiler to perform specific mathematical and logical operations. c ++ provides many available operators.
- C ++ loopthere is a situation where you need to make a code a few times. in general, statements are executed sequentially.
- Control flow in C ++flow control structures require the programmer to determine one or more conditions to be evaluated and checked by the program, along with the commands to be executed if the condition is determined to be correct, or other commands are done if the specified condition is wrong.
- Function in C / C ++a function is a group of commands that go together to perform a task. each c / c ++ program has at least one function, main (), and all most normal programs define additional functions.
- Call the function by value in C ++the method calls the function by the value of the parameters passed to a function that copies the actual value of a parameter into the official argument of the function. in this case, changes made to the parameter inside the function have no effect on the parameter.
- Call the function by pointer in C ++the method of calling a function by pointer in c ++ passes parameters to a function, copying the addresses of a parameter into the official parameter. inside the function, this address is used to access the actual parameter used in the function call. that is, changes made to the official parameter affect the passed parameter.