Storage-class specifiers
-
-
auto
- automatic duration with no linkage. -
register
- automatic duration with no linkage. Also hints to the compiler to place the variable in the processor's register. -
static
- static duration with internal linkage at file scope and no linkage at block scope. -
extern
- static duration with either internal or more usually external linkage. -
_Thread_local
- (since C11) - thread storage duration.
-
Contents |
[edit] Explanation
[edit] Storage duration
All variables in a program have one of the following storage durations that determines its lifetime:
-
-
automatic storage duration. The variable is allocated at the beginning of the enclosing code block and deallocated at the end. This is the default for all variables, except those declared
static
,extern
or_Thread_local
.
-
automatic storage duration. The variable is allocated at the beginning of the enclosing code block and deallocated at the end. This is the default for all variables, except those declared
-
-
static storage duration. The variable is allocated when the program begins and deallocated when the program ends. Only one instance of the variable can exist. Variables declared with
static
orextern
have this storage duration.
-
static storage duration. The variable is allocated when the program begins and deallocated when the program ends. Only one instance of the variable can exist. Variables declared with
-
-
thread storage duration (since C11). The variable is allocated when the thread begins and deallocated when the thread ends. Each thread has its own instance of the variable. Only variables declared
_Thread_local
have this storage duration._Thread_local
can only be declared for variables declared withstatic
orextern
and cannot be used in a function declaration.
-
thread storage duration (since C11). The variable is allocated when the thread begins and deallocated when the thread ends. Each thread has its own instance of the variable. Only variables declared
-
- allocated storage duration. The variable is allocated and deallocated per request by using dynamic memory allocation functions.
[edit] Linkage
Linkage refers to the ability of a variable or function to be referred to in other scopes. If a variable or function with the same identifier is declared in several scopes, but cannot be referred to from all of them, then several instances of the variable are generated. The following linkages are recognized:
-
-
no linkage. The variable can be referred to only from the scope it is in (block scope). All variables with automatic, thread and dynamic storage durations have this linkage, as well as variables declared
static
at block scope.
-
no linkage. The variable can be referred to only from the scope it is in (block scope). All variables with automatic, thread and dynamic storage durations have this linkage, as well as variables declared
-
-
internal linkage. The variable can be referred to from all scopes in the current translation unit. All variables which are declared at file scope have this linkage, including variables declared
static
at file scope.
-
internal linkage. The variable can be referred to from all scopes in the current translation unit. All variables which are declared at file scope have this linkage, including variables declared
-
-
external linkage. The variable can be referred to from any other translation units in the entire program. All variables which are declared either
extern
orconst
with no explicit storage-class specifier, but notstatic
, have this linkage.
-
external linkage. The variable can be referred to from any other translation units in the entire program. All variables which are declared either
[edit] Keywords
auto, register, static, extern, _Thread_local
[edit] Example
Scope, Visibility, Lifetime: A variable with automatic storage duration is allocated with a high memory address. A variable with static storage duration is allocated with a low memory address; uninitialized variables are separated from initialized variables in memory for a fast internal initialization to zero. A variable with allocated storage duration is allocated memory above variables with static storage duration.
#include <stdio.h> #include <stdlib.h> /* static storage duration */ int A; // uninitialized global variable; this system initializes with zero int B = 9; // initialized global variable int main(void) { printf("&A = %p\n", (void*)&A); printf("&B = %p\n", (void*)&B); /* automatic storage duration */ int A = 1; // hides global A printf("&A = %p\n", (void*)&A); /* static storage duration */ static int B=1; // hides global B printf("&B = %p\n", (void*)&B); /* allocated storage duration */ int *ptr_1 = (int*)malloc(sizeof(int)); /* start allocated storage duration */ printf("address of int in allocated memory = %p\n", (void*)ptr_1); free(ptr_1); /* stop allocated storage duration */ return 0; }
Possible output:
&A = 0x60103c &B = 0x601034 &A = 0x7fff3c3153fc &B = 0x601030 address of int in allocated memory = 0xc4f010