English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structure

C Language File

C Others

C Language Reference Manual

C Language Storage Classes

Storage class defines variables in C programs/The scope (visibility) and lifetime of functions. These specifiers are placed before the types they modify. Below is a list of storage classes available in C programs:

  • auto

  • register

  • static

  • extern

auto storage class

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

{
   int mount;
   auto int month;
}

The example above defines two variables with the same storage class. auto can only be used within a function, which means that auto can only modify local variables.

register storage class

register The storage class is used to define local variables that are stored in a register rather than RAM. This means that the maximum size of the variable is equal to the size of the register (usually a word), and the unary '&' operator cannot be applied to it (because it has no memory location).

{
   register int miles;
}

The register is used only for variables that need fast 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 the variable may be stored in a register, depending on hardware and implementation limitations.

static storage class

static The storage class indicates to the compiler that local variables should be kept in existence throughout the life of the program, without the need to create and destroy them each time they enter and leave scope. Therefore, using the static modifier for local variables can maintain the value of the local variable between function calls.

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

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

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

#include <stdio.h>
/* Function declaration */
void func1(void);
static int count = 12;        /* global variable - static is the default */
int main()
{
    while (count--) {
        func1();
    }
    return 0;
}
void func1(void)
{
    /* 'hello' is 'func1The local variable of ' - is initialized only once
    * Each call to the function 'func1'hello' value will not be reset.
    */
    static int hello = 4;
    hello++;
    printf(" %d , count is %d\n", hello, count);
}

In the example, count as a global variable can be used within the function, and hello using the static modifier will not be reset with each call.

You may not understand this example yet, because I have used functions and global variables, which are concepts that have not been explained so far. Even if you don't understand it completely now, it's okay; later chapters will explain them in detail. When the above code is compiled and executed, it will produce the following result:

 hello is 5 , count is 11
 hello is 6 , count is 10
 hello is 7 , count is 9
 hello is 8 , count is 8
 hello is 9 , count is 7
 hello is 10 , count is 6
 hello is 11 , count is 5
 hello is 12 , count is 4
 hello is 13 , count is 3
 hello is 14 , count is 2
 hello is 15 , count is 1
 hello is 16 , count is 0

extern storage class

extern Storage class is used to provide a reference to a global variable, which is visible to all program files. When you use extern For variables that cannot be initialized, the variable name will point to a previously defined storage location.

When you have multiple files and define a global variable or function that can be used in other files, you can use extern To get a reference to a defined variable or function. It can be understood like this,extern It is used 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 variable or function, as shown below:

First file: main.c

#include <stdio.h>
 
int count ;
extern void write_extern();
 
int 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 second file contains extern Keyword is used to declare that has been defined in the first file main.c count. Now, compile these two files as follows:

 $ gcc main.c support.c

This will produce a.out Executable program, when the program is executed, it will produce the following results:

count is 5