Storage Classes in C
Any variable in C programming is specified by it's data-type and storage-class specifier.
The general syntax is as follows :-
- //storage-class data-type variable-name;
- // Example:
- extern int myInteger;
There are four storage classes in C:
3) Static, and
1.1) Automatic Variables :-
These are the variables defined with in a function. Alternatively any thing(variable) defined within a function is automatic. They are identified by the keyword 'auto'. The general syntax is as follows :
- auto int k;
As these variables are identified by there location in the program, the keyword auto is not included in general practice. There for anything defined within a function is automatic unless no other specifier is included.
- int myFunction()
- int a; //an automatic variable
- auto int b; //another automatic variable
- /*Other Operations */
1.2) Scope of Automatic Variables :-
The are confined to the function of there declaration. Alternatively, they can not be accessed outside the function in which they are defined. In fact, there scope can be further limited by defining them within a block. For example see the following code:
The values of the automatic variables are lost as soon as the function is exited i.e. these variables do not retain their values once the function returns. When the function is called next time, they are used with the new values, not the value assigned in last function call.
1.3) Their initialization :-
We can initialize automatic variables by including appropriate expressing in their definition/declaration as follows:
- int k = 0;
- auto long num = 100;
The initialization can also be done explicitly as follows:
- int k; //declaration
- k = 0; //initialization
When not initialized the contain garbage values by default. In order to avoid any malfunctioning of the program, initialization is a good practice in general.
Next will be about external variables...
2.1) External Variables :-
These are the variables that are defined outside any function. Alternatively, any thing defined outside of any function is external variable. Therefore, it can be said that like automatic variable, their scope is also determined by their position of definition.
They are identified by the keyword 'extern'.
General syntax of their definition is as follows:
- extern int k; //an external variable
However, the keyword extern is not compulsory as these variables are identified by their location. A variable defined outside any function is external by default. In fact, some compilers forbid the use of keyword extern.
2.2) Scope of External Variables :-
Their scope spans the life of the program i.e., they can be accessed from anywhere in the program through out the life of program. Thus, we can modify these variable in any function any can access these values from another function.
Therefore, these variable provide a means for communication between two function i.e., we can pass values from one function to other using these variables in efficient way. Specially, when a large number of input data items, these play a vital role for this purpose. In general, we can pass values to a function without using arguments by external variables.
Let's see an example :-
- extern int i; // we may exclude keyword 'extern'
- int n = 0;
- /* Other variables */
- i = 50;
- int function1()
- i = 10;
- printf("In function1 value of i = %d\n", i);
- return 0;
- int function2()
- auto int i;
- i = 20;
- printf("In function2 value of i = %d\n", i);
- return 0;
When this program runs will give following output :-
- In main i = 50
- In function1 value of i = 10
- In main i = 10
- in function2 value of i = 20
- In main i = 10
Explanation is as follows :-
First line is the result of assignment i = 50; in main(). Second line is the modified value of i in the function1(). Third line is the result of assignment in fumction1(). As i is exposed everywhere in the program, it has been modified by function1(). Fourth line is the result of assignment i = 20; in function2(). But we should know that function2() itself contains a definition of i. Thus this i is local to function2(), thus this change will not be reflected elsewhere. This variable i, is different from global i.Therefore this assignment in function2() does not affect external variable i. But, generally this is a bad programming practice as it can create confusions. We should used a different names unless we are restricted by the situations.
Last line is result of last printf statement in main().
1. If a function requires an external variable defined earlier in the program, may freely assess it without any explicit declaration(Remember that any changes made will be reflected globally).
2. If a function definition precedes the external variable definition, the function must include declaration for that external variable.
3. An external variable declaration must begin with keyword extern.
4. External variable declaration can not include initialization statement.
5. No space is allocated due to declaration of external variables.
These are few difference between definition and declaration of external variables.
2.3) Initialization :-
1. There definition can include assignment expression or can be initialized explicitly.
2. When not initialized manually, they are initialized to 0(zero).
3. Their declaration must not contain initialization.
4. Initialization assignment must be done using constant and not using expressions.
Next will be Static variables...