Vous êtes sur la page 1sur 3

Storage classes:The storage class of a variable dictates how, when and where storage will be allocated for the

variable. The different storage classes available are: 1. Auto 2. Register 3. Extern 4. Static

1.Auto: Automatic variables are declared inside a function in which they are to be utilized. They are created when the function is called and destroyed automatically when the function is exited, hence the name automatic. Automatic variables are therefore private (or local) to the function in which they are declared. Because of this property, automatic variables are also referred to as local or internal variables. A variable declared inside a function without storage class specification is, by default, an automatic variable. One important feature of automatic variables is that their value cannot be changed accidentally by what happens in some other function in the program. This assures that we may declare and use the same variable name in different functions in the same program without causing any confusion to the compiler. 2. Register: It is possible for use to attribute the register storage class to certain variables. We can tell the compiler that a variable should be kept in one of the machines registers, instead of keeping in the memory (where normal variables are stored). Since a register access is much faster than a memory access, keeping the frequently accessed variables in the register will lead to faster execution of programs. This is done as follows: register int count; The important point while using register variables is, registers of CPU do not have addresses. Thus, we should not refer the address of a register variable.

3.Extern: This is the default storage class for all global variables. Extern storage class variables get initialized (to zero in the case of integers) automatically, retain their value throughout the execution of the program and can be shared by different modules of the same program. However, assuming int intvar;is present in a.c., to be also to have proper binding with the same variable, b.c(another file) should have extern int intvar.

4. Static: The static storage class has a number of implications depending upon its usage. (a) The default storage class for all local variables is auto. This can be changed to static by prefixing the declaration with the keyword static as in static int intvar. A local variable with static storage class is still a local variable as far as its scope is concerned, it is still available only inside the function in which it is declared. But certain other properties of the variable change; It gets initialized to zero automatically, is initialized only once, (during program startup) and it retains its value throughout the execution of the program. (b) When applied to a global variable, the global variable becomes inaccessible outside the file in which it is declared. (c) This storage class can be applied to functions too. Similar to case 2, the functions become inaccessible outside the file.

Vous aimerez peut-être aussi