Storage class specifiers in c

Storage class specifiers in c

The Storage class specifiers in c refer to how widely it is known among a set of function in a program. In other words, how the memory reference is carried out for a variable. Every identifier in C/C++ not only has a type such as integer, double and so on but also a storage class that provides information about its visibility, lifetime and location. For example, if the variable belongs to an automatic type, it means whenever the variable is used in a main program or a function, the computer will automatically reserve a memory space for it. Normally, in C/C++, a variable can be declared as belonging to any one of the following groups:

In C programming, storage class specifiers are keywords that define the visibility and lifetime of variables. These specifiers indicate how a variable should be stored in memory and determine its scope and duration during program execution. There are four main storage class specifiers in C: auto, register, static, and extern.

  1. Automatic variable
  2. Register variable
  3. Static variable
  4. External variable
Storage class specifiers in c
Storage class specifiers in c
  • Automatic Variable:

Internal or local are the variables, which are declared inside a function. Instead, they are more often referred to as automatic due the fact that their memory space is automatically allocated as the function is entered and released soon as it leaves. In other words, automatic variables are given only temporary memory space. They have no meaning outside the function in which they are declared. The portion of the program where a variable can be used is called the scope of that variable. Automatic variables can be declared not only at the beginning of a function but also at the beginning of a compound statement (also called a block).

  • Register Variable:

Automatic variables are stored in the memory. As accessing a memory location takes time (much more time than accessing one of the machine’s registers), one can make the fast computer to keep only a limited number of variables in their registers for fast processing. Whenever some variables are to be read or repeatedly used, they can be assigned as register variables.

The general syntax of a register variable is.

register datatype variable 1, variable 2..variable_n;

The keyword register is used to declare that the storage class of the variable is a register type. For a limited number of variables it is possible to make the computer to keep them permanently in fast registers. Then the keyword register is added in their declaration

  • Static Variable:

Static variables are defined within a function and they have the same scope rules of the automatic variable but in the case of static variables, the contents of the variables will be retained throughout the program

  • External Variable:

Variables which are declared outside the main are called external variables and these variables will have the same data type throughout the program, both in the main and in the functions. Normally, variables are declared only at the beginning of blocks. But one can also declare variables outside, which resemble the global variables of Pascal and the common variables of FORTRAN, simply by referring to them by their name. Global variables have the same life time and initialization rules as the static variables. This implicit initialization is convenient and is taken advantage of in countless programs, but explicitly initializing global variables makes programs more readable.

The general syntax of the extern variable is,

extern datatype variable-1, variable 2… variable n;

where extern is a keyword used to define the storage class as external variables.

The following declarations are valid

extern int x, y;

extern float p,q,r;

extern char al,ch;

Leave a Comment