Skip to content

Declarations

Properties of variables

  • Every variable in a C program has three properties:

Storage duration

  • Automatic storage duration: is allocated when the surrounding block is executed; storage is deallocated when the block terminates, causing the variable to lose its value (no default init value).
  • Static storage duration: variable can stay at the same storage location as long as the program is running, allowing it to retain its value indefinitely.

Scope

  • Block scope: the variable is visible from its point of declaration to the end of the enclosing block
  • File scope: the variable is visible from its point of declaration to the end of the enclosing file.

Linkage

  • External linkage: the variable may be shared by several files in a program.
  • Internal linkage: the variable is restricted to a single file, but may be shared by the functions in that file.
  • No linkage: the variable belongs to a single function and can’t be shared at all.

Storage classes

auto storage class

  • The auto storage class is legal only for variables that belong to a block. An auto variable has automatic storage duration (not surprisingly), block scope, and no linkage
  • Default for variables declared inside a block.

static storage class

static int i;
// static - file scope - internal package
void f(void)
{
static int j;
// static - block scope - no linkage
}

extern storage class

  • The extern storage class enables several source files to share the same variable.
extern int i;
// static - file scope - ? linkage
void f(void)
{
extern int j;
// static - block scope - ? linkage
}
/*
If the variable was declared static earlier in the file (outside of any function definition), then it has
internal linkage. Otherwise (the normal case), the variable has external linkage
*/

register storage class

Function storage class

Example

int a;
extern int b;
static int c;
void f(int d, register int e)
{
auto int g;
int h;
static int i;
extern int j;
register int k;
}
NameStorage durationScopeLinkage
astaticfileexternal
bstaticfilex
cstaticfileinternal
dautomaticblocknone
eautomaticblocknone
gautomaticblocknone
hautomaticblocknone
istaticblocknone
jstaticblockexternal
kautomaticblockx

Declarators

  • Deciphering complex declarations:

  • Using type definitions to simplify declarations