Storage class of a variable explains the lifetime and reflectivity of a variable. That means the period till which the variable rests active and visibility describes in which module of the program the variable is manageable.
The storage class of an object is understood by
■ The position of its announcement in the source file
■ The storage class specifier, which can be provided optionally.
When an object is declared, storage class is also considered. As it specifies the period of time from the construction of the object till its demolition.
Automatic Storage Class
• Automatic storage class allocates a variable to its default storage type.
• auto keyword is used to announce automatic variables.
• In case, a variable is stated without any keyword inside a function, it is automatic by default.
• This variable is visible only within the function it is confirmed and its lifetime is same as the lifetime of the function as well. Once the execution of function is over, the variable is demolished.
• The storage class autocontains all those objects well-defined within a function but without the static keyword. The constraints of a function are also auto objects.
• auto objects have no exact initial value if they are not make ready explicitly. Yet, objects belonging to a class type are usually initialized with default values, which can be identified in the class definition.
datatype var_name1 [= value]; Or else auto datatype var_name1 [= value];
auto int x; float y = 5.67;
Register Storage Class
• Register storage allocates a variable’s storage in the CPU registers than primary memory. It possesses its lifetime and visibility as like. Automatic variable.
• The determination of making register variable is to upsurge access speed and makes program run quicker.
• If there is no space accessible in register, these variables are stored in main memory. The variables which needs fast access should be made register.
register datatype var_name1 [= value];
register int id; register char a;
• g is a global variable,
• s is static,
• r is register
• a is automatic variable. There are two functions, main() and test_function().
2. External Storage Class
• External storage class gives variable a reference to a global variable.it is stated outside the given program.
• extern keyword is used to announce external variables.
• They are visible all over the program and its time is same as the lifetime of the program where it is acknowledged. This visible to all the functions existing in the program.
• If the keyword static is not used when telling a function, the function must fit in to the extern storage class. The external functions can be used at any place in a program. If you need to call a function before defining it, or in another source file, you will must to declare that function.
externboolgetPassword(void); // Prototype
here, you can neglect the extern keyword, since functions belong to the extern storage class by default.
extern datatype var_name1;
extern float var1;
Here,A variable test is stated as external in main.cpp. it is allotted to 100 in sub.cpp. It can be retrieved in both files. The function multiply() multiplies the value of test with the parameter passed to it while raising it. The program carry out the multiplication with making changes the global variable test to 500.
Note: Run the main.cpp program
3. Static Storage Class
• Static storage class make sure a variable has the visibility mode of a local variable but lifetime of an external variable.
• It can be used only within the function where it is acknowledged .it isdamaged only after the program execution has completed.
• after a function call, the variable well-defined as static inside the function retains its previous value and works on it. This is frequently used to save values in a recursive function.
static datatype var_name1 [= value];
static int x = 101; static float sum;