Easy Tutorial
❮ C Function Atoi C Function Memcpy ❯

C Storage Classes

Storage classes define the scope (visibility) and lifetime of variables/functions in a C program. These specifiers are placed before the types they modify. The following are the storage classes available in a C program:

auto Storage Class

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

{
   int mount;
   auto int month;
}

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

register Storage Class

The register storage class is used to define local variables that should be stored in a register instead of RAM. This means 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;
}

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

static Storage Class

The static storage class instructs the compiler to keep a local variable in existence during the life of the program, without needing to create and destroy it each time it comes into and goes out of scope. Therefore, using static to modify a local variable allows it to maintain its value between function calls.

The static modifier can also be applied to global variables. When static is used on a global variable, it limits the variable's scope to the file in which it is declared.

A globally declared static variable or method can be called by any function or method, as long as these methods appear in the same file as the static variable or method.

The following example demonstrates the application of static to global and local variables:

Example

#include <stdio.h>

/* Function declaration */
void func1(void);

static int count=10;        /* Global variable - static is default */

int main()
{
  while (count--) {
      func1();
  }
  return 0;
}

void func1(void)
{
/* 'thingy' is a local variable of 'func1' - initialized only once
 * The value of 'thingy' will not be reset with each function call.
 */                
  static int thingy=5;
  thingy++;
  printf(" thingy is %d, count is %d\n", thingy, count);
}

In this example, count as a global variable can be used within the function, and thingy, when modified with static, will not reset with each call.

You might not fully understand this example now, as we have used functions and global variables, concepts not yet explained. Even if you don't understand it completely now, it's okay, as we will cover these topics in detail in subsequent chapters. When the above code is compiled and executed, it produces the following result:

thingy is 6, count is 9
thingy is 7, count is 8
thingy is 8, count is 7
thingy is 9, count is 6
thingy is 10, count is 5
thingy is 11, count is 4
thingy is 12, count is 3
thingy is 13, count is 2
thingy is 14, count is 1
thingy is 15, 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', you cannot initialize the variable as it points to a storage location that has been previously defined.

When you have multiple files and you define a global variable or function that can be used in other files, you can use extern in another file to reference the already defined variable or function. Think of extern as a way to declare a global variable or function in another file.

The extern modifier is typically used when there are two or more files sharing the same global variables or functions, as shown below:

First file: main.c

Example

#include <stdio.h>

int count ;
extern void write_extern();

int main()
{
   count = 5;
   write_extern();
}

Second file: support.c

Example

#include <stdio.h>

extern int count;

void write_extern(void)
{
   printf("count is %d\n", count);
}

Here, the extern keyword in the second file is used to declare the count that was defined in the first file, main.c. Now, compile these two files as follows:

$ gcc main.c support.c

This will produce the a.out executable program, which, when executed, will produce the following result:

count is 5
❮ C Function Atoi C Function Memcpy ❯