storage classes in c programming

The scope or visibility of variables within a c program is called storage classes in c programming.

In other words, you can say that the lifetime of variables is defined by a storage class. Storage classes in c programming tell the compiler about the execution life of variables and functions within a program. There are four types of storage classes in c programming.

Types Of Storage Class

  • auto storage class
  • register storage class
  • static storage class
  • extern storage class

auto storage class

The auto storage class is the default storage class for all local variables. The most important thing is ‘auto’ storage class can only be used for local variables or you can say it can be used within the function. Though, there is no difference between declaring a variable directly or by prefixing the variable name by auto within a function.

For example,

DataType_of_function function_name( dataType_of_argument)
{
int a;
auto int a;
}

In the above example, there is no difference between both variables and this is a valid representation.

Invalid representation of auto storage class

auto int b;
DataType_of_function function_name( dataType_of_argument)
{
int a;
}

The auto storage class can only be used for local variables. Variable b is not a local variable instead it is a global variable. Hence the above representation is an invalid representation.

register storage class

The local variables that are stored inside a register instead of RAM(Random Access Memory) are of register storage class.  These variables are prefixed with register keyword. In other words, you can say that these variables have 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).

The register should only be used for variables that require quick access such as counters. It’s most important to notice that defining ‘register’ does not mean that the variable will be stored in a register. Instead, it means that it MIGHT be stored in a register depending on hardware and implementation restrictions.

For example,

DataType_of_function function_name( dataType_of_argument)
{
register int speedOfLigth;
}

static Storage Class

The static storage class tells the compiler to keep a local variable in existence during the lifetime of the execution of 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.

Have You enjoyed the article on storage classes in c programming!

For example,

#include <stdio.h>
void CheckFunction(void);          /* function declaration */
static int count = 4;                       /* global variable */
main()
{
 while(count--)
   {
     CheckFunction();   /* function call */
   }
return 0;
}

/* function definition */
void CheckFunction( void )
{
  static int a = 4;              /* local static variable */
  a++;
  printf("a is %d and count is %d\n", a, count);
}

OUTPUT:

  • First Line: a is 5 and count is 3
  • Second Line: a is 6 and count is 2
  • Third Line: a is 7 and count is 1
  • Fourth Line: a is 8 and count is 0

extern storage class

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 several files in your project and you have defined a global variable or function, which is also be used in other files, then extern will be used in another file to provide the reference of defined variable or function.

For example,

First File first.c

#include <stdio.h>
int externVariable ;  /* externVariable is a global variable */
extern void check_extern(); /* check_extern function declaration */
main()
  {
   externVariable = 5;   /* externVariable is assigned some value */
   check_extern();        /* check_extern function call */
  }

Second File second.c

#include <stdio.h>
extern int externVariable;
void write_extern(void)
  {
  printf("externVariable is %d\n", externVariable);
  }

Here, extern is used to declaring count in the second file, whereas it has its definition in the first file, first.c.

OUTPUT:

externVariable is 5


<< Prev                                                                                                              Next >>


 

Detected Ad-Blocking Software!

Hello Folks!

We run this website to provide quality content to our visitors. For the betterment & maintenance of the website, we need money. We earn money through Adsense on free quality content. We know most of you are our prime members. But this is just a global detection. If you are our premium member then you won't see any ad on the pages. 

Please Co-operate with us by enabling our website to serve Ads!

Best Wishes!

Mycteacher Team

Close