Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

C Storage class


May 11, 2021 C


Table of contents


C Storage class

The storage class defines the range (visibility) and lifecycle of variables/functions in the C program. T hese descriptors are placed before the type they decorate. The storage classes available in the C program are listed below:

  • auto
  • register
  • static
  • extern

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, that is, auto can only modify local variables.

Register storage class

The register storage class is used to define local variables stored in registers instead of RAM. This means that the maximum size of the variable is equal to the size of the register (usually a byte) and cannot be applied to it by the one-dollar operator (because it has no 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 that the variable will be stored in the register, it means that the variable may be stored in the register, depending on the hardware and implementation limitations.

static storage class

The static storage class instructs the compiler to maintain the existence of local variables throughout the life cycle of the program without having to create and destroy them each time it enters and leaves the scope. Therefore, using static to decorate local variables keeps the value of local variables between function calls.

Static modifiers can also be applied to global variables. When static decorates a global variable, the scope of the variable is limited to the file that declares it.

In C programming, when static is used on a class data member, only one copy of that member is shared by all objects of the class.

#include <stdio.h>
 
/* 函数声明 */
void func(void);
 
static int count = 5; /* 全局变量 */
 
main()
{
   while(count--)
   {
      func();
   }
   return 0;
}
/* 函数定义 */
void func( void )
{
   static int i = 5; /* 局部静态变量 */
   i++;

   printf("i is %d and count is %d\n", i, count);
}

Maybe you can't understand this example right now, because I've used functions and global variables, which haven't been explained yet. E ven if you don't fully understand it now, it doesn't matter, we'll cover it in more detail in subsequent chapters. When the above code is compiled and executed, it produces the following results:

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 storage class

The extern storage class is used to provide a reference to a global variable that is visible to all program files. When you use 'extern', for variables that cannot be initialized, the variable name points to a previously defined storage location.

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 other files to get a reference to a defined variable or function. Understandably, extern is used to declare a global variable or function in another file.

Extern modifiers are typically used when two or more files share the same global variable or function, as follows:

First file: main .c

#include <stdio.h>
 
int count ;
extern void write_extern();
 
main()
{
   count = 5;
   write_extern();
}

Second file: support .c

#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 count that has been defined .c the first file main. Now, compile the two files as follows:

 $gcc main.c support.c

This results in an a.out executable, and when the program is executed, it produces the following results:

count is 5