Saturday, December 31, 2011

C Storage Classes & Scope

Discuss available storage classes in C and relevent scope details

1.Auto
2.Register
3.Extern
4.Static

Auto is the default storage class for local variables.
 {
     int Count;
     auto int Month;
 }
The example above defines two variables with the same storage class. auto can only be used within functions, i.e. local variables. 


Register is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and cant have the unary '&' operator applied to it (as it does not have a memory location).
 {
   register int  Miles;
 }
Register should only be used for variables that require quick access - such as counters. It should also be noted that defining 'register' goes not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register - depending on hardware and implementation restrictions.

1 comment: