storage class specifiers in C++.
The storage class specifiers are used to change the way of creating the memorystorage for the variables.
auto:
This auto specifier tells the compiler that the variable declared will go out of scope once the program exits from the current block. The program block can bea function, a class or a structure.This is the most widely used and non-used storage class specifier in C++.Because, all the variables declared in C++ are of the type auto by default. So noone need to worry about specifying this one. The declarations,auto int var1; // declared with auto specifier for the c++ tutorialint var1; //declared without the storage class specifier Both the above declarations will produce the same result.
static:
This static specifier when used will preserve the value for a particular variableupon re-entry into the same function. For example //C++ Tutorial - Example code for demonstrating static variablevoid static_function_example(){static int x = 0; //variable for C++ tutorial examplex++;cout << x <<endl;}If this function is called 10 times, the output will be 1,2,3,4..etc., The value of the variable x is preserved through function calls.If this static variable is declared as a member of a class, then it will preservethe value for all the objects of the class.i.e, one copy of this data variable will beshared by all objects of the class.
extern:
This extern keyword is used to specify that the variable is declared in adifferent file. This is mostly used to declare variables of global scope in C++projects. When the keyword extern is used, the compiler will not allocatememory for the variable. Programmers in C++ would have very frequently facedlinker errors because of wrong external linking.
Add a Comment