C Control StatementsC FunctionsC ArrayC PointerC Dynamic MemoryC StringC MathC StructureC UnionC File HandlingC PreprocessorC Command LineC Misc |
C storage classesIn additional to data type, every variable has one or more attributes known as storage class or storage class are used to determine the memory location, visibility and the default or initial value of the variable. The general syntax
There are four types of storage classes in c language .
with the use of storage classes keywords auto, static, register, extern. so we can write declaration statements like this
When explicitly the storage class specifier is not present in declaration, compiler automatically assume the default storage classes on the based on declaration. A storage class are determine about these four aspects of a variable
Automaticwhen all the variable declare any where inside a function/block without any storage class specifier are automatic variable. we can also use auto keyword to declare automatic variables. in this example both function have local variables both are equals
RegisterThe register storage class can only apply on automatic variables. The scope of variable is lifetime and initial value of register variable is same as automatic variable. but only difference between automatic and register variable is in the place where it is stored. Automatic variables are stored in memory but the register variables are stored in cpu registers, but registers can stored only small memory in the processor. The variables stored in the register can be accessed much faster than the variables stored in memory
But Register variables do not have memory address so we cannot apply (&) operator to find the address of register variable. ExternalVariables that have to be use by many functions and also use by different files can be declare as external variables. The default value of external variable is zero.
Definition of an external variable
1. The definition create the variable, so at the definition time memory is allocated to the variable. Declaration of an external variable
1. Declaration can not create variable so at the time of declaration memory can not allocated.
In this above example the variable a avalaibe for all functions |