6.15.1. Storage Modifiers

Storage modifiers are keywords that change a variable's primary or default behavior. C and C++ include four modifiers. The first two modifiers were introduced previously but 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 are auto or automatic by default, so in practice, programmers only use the keyword with type deduction. They define automatic variables inside functions, explaining their alternate name: local variables. The program allocates memory for automatic variables when the variable comes into scope and deallocates it when it goes out of scope, making it difficult to make the distinction between them (please see scope vs. memory allocation for a detailed discussion of the distinction). 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.
register. Programmers use the register modifier to suggest that a program uses a variable often enough that maintaining it in a CPU register, rather than memory, will improve the program's efficiency. However, the number of available CPU registers is relatively small, varying between manufacturers and models, so the keyword was never more than a suggestion, which the compiler could ignore. Modern compilers incorporate register optimization algorithms that generally perform better than 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.