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 :-
  1. //storage-class data-type variable-name;
  2. // Example:
  3. extern int myInteger;
There are four storage classes in C: 1) Automatic 2) External 3) Static, and 4) Register. 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 :
  1. 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.
  1. int myFunction()
  2. {
  3. int a; //an automatic variable
  4. auto int b; //another automatic variable
  5. .
  6. .
  7. /*Other Operations */
  8. }
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:
  1. int main()
  2. {
  3. int k; //an automatic variable
  4. char ch; //another automatic variable
  5. {
  6. int n; //automatic variable within current block;
  7. n = 100;
  8. printf("%d", n); // Correct
  9. }
  10. n = 90; //Wrong, n is not declared
  11. printf("%d", n); //Wrong, n is not declared.
  12. }
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:
  1. int k = 0;
  2. auto long num = 100;
The initialization can also be done explicitly as follows:
  1. int k; //declaration
  2. 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:
  1. 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 :-
  1. extern int i; // we may exclude keyword 'extern'
  2.  
  3. main()
  4. {
  5. int n = 0;
  6. .
  7. .
  8. /* Other variables */
  9. i = 50;
  10. printf("In main i = %d\n", i);
  11. function1();
  12. printf("In main i = %d\n", i);
  13. function2();
  14. printf("In main i = %d\n", i);
  15. return;
  16. }
  17.  
  18.  
  19.  
  20. int function1()
  21. {
  22. i = 10;
  23. printf("In function1 value of i = %d\n", i);
  24. return 0;
  25. }
  26.  
  27. int function2()
  28. {
  29. auto int i;
  30. i = 20;
  31. printf("In function2 value of i = %d\n", i);
  32. return 0;
  33. }
When this program runs will give following output :-
  1. In main i = 50
  2. In function1 value of i = 10
  3. In main i = 10
  4. in function2 value of i = 20
  5. 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(). NOTE ;- 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...

Tags

Add new comment