Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Storage Classes

C Storage Classes

A storage class defines the storage location, lifetime, and scope of variables/functions in a C program.

These specifiers are placed before the type they modify.

The following storage classes are available in C programs:

auto Storage Class

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

Variables defined inside a function default to the auto storage class, which means they are created when the function begins and destroyed when the function ends.



{

   int mount;

   auto int month;

}

The example above defines two variables with the same storage class; auto can only be used inside functions, that is, auto can only modify local variables.

register Storage Class

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

The register storage class defines variables stored in registers, so access is faster, but you cannot take their address directly because they are not stored in RAM. Using the register storage class on frequently accessed variables can improve program speed.



{

   register int  miles;

}

Registers are only used for variables that need fast 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 constraints.

static Storage Class

static The static storage class instructs the compiler to preserve the existence of local variables throughout the lifetime of the program, without creating and destroying them each time they enter and leave scope. Therefore, using static to modify a local variable preserves the value of the local variable between function calls.

The static modifier can also be applied to global variables. When static modifies a global variable, it limits the scope of the variable 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 those methods appear in the same file as the static variable or method.

A static variable is initialized only once in a program; even if the function is called multiple times, the variable's value is not reset.

The following example demonstrates the use of static with global and local variables:

Example

#include

<

stdio.h

/*

Function declaration

*/

void

func1

(

void

)

;

static

int

count

=

10

;

/*

Global variable - static is the default

*/

int

main

(

)

{

while

(

count

--

)

{

func1

(

)

;

}

return

0

;

}

void

func1

(

void

)

{

/*

'thingy' is a local variable of 'func1' - initialized only once * each time 'func1' is called, the value of 'thingy' is not reset.

*/

static

int

thingy

=

5

;

thingy

++;

printf

(

"

thingy is %d, count is %d

\

n

"

,

thingy

,

count

)

;

}

In the example, count is a global variable that can be used inside the function, and thingy, after being modified by static, is not reset on each call.

You may not yet understand this example because I have used functions and global variables, two concepts that have not been explained so far. Even if you cannot fully understand it now, that's fine; we will explain it in detail in later chapters. When the code above is compiled and executed, it produces the following result:



 thingy 为 6 , count 为 9

 thingy 为 7 , count 为 8

 thingy 为 8 , count 为 7

 thingy 为 9 , count 为 6

 thingy 为 10 , count 为 5

 thingy 为 11 , count 为 4

 thingy 为 12 , count 为 3

 thingy 为 13 , count 为 2

 thingy 为 14 , count 为 1

 thingy 为 15 , count 为 0

extern Storage Class

extern The extern storage class is used to define global variables or functions declared in other files. When the extern keyword is used, no storage is allocated for the variable; it simply tells the compiler that the variable is defined in another file.

extern The 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 a variable that cannot be initialized, the variable name is made to refer 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 in other files to get a reference to the already-defined variable or function. It can be understood as follows:extern is used to declare a global variable or function in another file.

The extern modifier is usually used when two or more files share the same global variable or function, 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 The keyword is used to declare the variable already defined in the first file, main.c count. Now, compile these two files as follows:



 $ gcc main.c support.c

This produces a.out executable program; when the program is executed, it produces the following result:



count is 5