6.15.1. Storage Modifiers

variable, auto, static, extern, register, volatile, type deduction

Storage modifiers are keywords that change a variable's primary or default behavior. C and C++ include four modifiers. Described previously, the auto and static keywords are included here for completeness.

void print(double p, double r, int n)
{
	double total_interest = 0.0;	// initialized on every function entry
	double total_principal = 0.0;

	for (int i = 1; i <= n; i++)
	{
			.
			.
			.
	}
}
auto. All variables defined in a function are auto or automatic by default, so in practice, programmers only use the keyword with type deduction. The scope of variables defined in a function is restricted to the local statements in the function, which explains why automatic variables are also called local variables. The program allocates memory for automatic or local variables when the variable comes into scope and deallocates it when it goes out of scope, making it difficult to see the distinction between the terms scope and memory allocation. The program explicitly initializes automatic variables (with the assignment operator) each time it calls the defining function.

 

double random()
{
	static double x = 0;	// initialization happens once when the OS loads the program for execution

	x = x * (x + 1) % 2147483648L;
	return  x;
}
static. The program allocates memory for and initializes static variables once at program startup. The memory remains allocated throughout program execution, "remembering" saved values between function calls. The program only deallocates the memory when it terminates.

 

Global VariablesPre-ANSI-C Functions
DefinitionsDeclarationsDefinitionsDeclarations
char c;
int x = 10;
extern char c;
extern int x;
T0 function(p1, p2, p3)
T1 p1,
T2 p2
{ /* function body */ }
extern T0 function();
(a)(b)
extern. The extern keyword distinguishes between definitions and declarations. In the past, it provided essential behaviors for variables and functions in C programs, but its usefulness has declined in C++ and ANSI C.
  1. Global variables must have exactly one definition in a program. When the program is spread across multiple files, each file using the variable must declare it. The extern keyword tells the compiler the variable is defined in an "external" file, distinguishing its definition from its declarations. Classes eliminate the need for global variables in most cases, reducing the use of extern in C++ programs.
  2. The original, pre-ANSI C function syntax didn't specify parameter types; instead, it specified them between the function's header and body. Pre-ANSI C syntax also didn't include function prototypes; instead, it used the extern keyword to denote a declaration. Programmers can optionally initialize variables in definitions, but cannot initialize them in declarations. The figure uses Tn to denote various unspecified data types.

 

register. Programmers use the register modifier to suggest that a program uses a variable often enough that keeping it in a CPU register, rather than memory, will improve efficiency. However, the number of available CPU registers is relatively small and varies by manufacturer and model, so the keyword was never more than a suggestion the compiler could ignore. Modern compilers incorporate register-optimization algorithms that generally outperform programmers at selecting the best variables to store in registers, so programmers rarely use this keyword today.

 

volatile. Programmers denote variables subject to change by entities outside the program (e.g., sensors, communication links, etc.) with the volatile keyword, instructing the program to load the saved value from memory whenever it uses the variable. The compiler does not include volatile variables in its register optimizations. Only specialized programs use the volatile modifier.