Storage Classes in C program


Variables and datatypes are discussed above. Now we will see how variables can be categorized by its scope and visibility.

Scope: Generally, scope is a term which signifies the lifetime of a variable. How long it will work and when it will be destroyed.

Visibility: Visibility shows from where the variable is visible, where can we use the variables. as an example if we use local variables we cannot use it from another functions or files, so it is visible only inside the block.

Block: Block is defined as set of lines between two curly braces {…}. as example

{
   //line1
   //line2
   //line3
}

This is a block.

Storage ClassHow has DeclaredScopeVisibility
autoGlobally

autoLocallyBlockBlock
registerGlobally

registerLocallyBlockBlock
staticGloballyProgramFile
staticLocallyProgramBlock
externGloballyProgramProgram
externLocallyProgramBlock

Basic Syntax for the storage class:

<storage class> <datatype> variable_name;
ex. static int my_var = 0;

Note: If we use like <datatype> variable_name; without specifying any storage class, it will be ‘auto’ storage class automatically.

Auto: The auto storage class is the default storage class for all local variables.

{
   int mount;
   auto int month;
}

The example above defines two variables with in the same storage class. 'auto' can only be used within functions, i.e., local variables.

Register: The register storage class is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and can't have the unary '&' operator applied to it (as it does not have a memory location).

{
   register int miles;
}

The register should only be used for variables that require quick access such as counters. It should also be noted that defining 'register' does not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register depending on hardware and implementation restrictions.

Static: The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope. Therefore, making local variables static allows them to maintain their values between function calls.

The static modifier may also be applied to global variables. When this is done, it causes that variable's scope to be restricted to the file in which it is declared.

In C programming, when static is used on a global variable, it causes only one copy of that member to be shared by all the objects of its class.

Example Code


#include <stdio.h>
/* function declaration */
void func(void);
static int count = 5; /* global variable */
main() {
   while(count--) {
      func();
   }
   return 0;
}
/* function definition */
void func( void ) {
   static int i = 5; /* local static variable */
   i++;
   printf("i is %d and count is %d
", i, count); }

Output

i is 6 and count is 4
i is 7 and count is 3
i is 8 and count is 2
i is 9 and count is 1
i is 10 and count is 0

Extern: The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern', the variable cannot be initialized however, it points the variable name at a storage location that has been previously defined.

When you have multiple files and you define a global variable or function, which will also be used in other files, then extern will be used in another file to provide the reference of defined variable or function. Just for understanding, extern is used to declare a global variable or function in another file.

The extern modifier is most commonly used when there are two or more files sharing the same global variables or functions as explained below.

Example Code (main.c):

#include <stdio.h>
int count ;
extern void write_extern();
main() {
count = 5;
write_extern();
}
Example Code (support.c):
#include <stdio.h>
extern int count;
void write_extern(void) {
printf("count is %d
", count); }

Output

count is 5

Updated on: 30-Jul-2019

139 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements