Storage Classes in C - Types And Classification Languages Programming Tutorials
http://ingenuitydias.blogspot.com/2015/11/storage-classes-in-c-types-and.html
Storage Classes in C-A storage class defines the scope (visibility) and life time of variables and/or
functions within a C Program..Two types of storage classes are there in c as explained below
1. Auto
2. Static
Storage class specifiers are
1. Auto
2. Static
3. Extern
4. Register
Auto - Storage Class-Auto is automatic class. resides in stack.
auto is the default storage class for all 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.
Variable Function Structure Pointer in C
Programming - C Viva Questions
Function in Embedded C
C Programming Basic Questions
Array in Embedded C
Arrays And Pointer in C
Register - Storage Class-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).Fast access but limited.Reside in CPU registers details.
{
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.
Static - Storage Class-static is the default storage class for global variables. The two variables below (count and road) both have a static storage class.
Static has three means
static variable
static global
static function
resides in data memory and BSS
static int Count;
int Road;
{
printf("%d ", Road);
}
Static variables can be 'seen' within all functions in this source file. At link time, the static variables defined here will not be seen by the object modules that are brought in.Static can also be defined within a function. If this is done the variable is initialized at run time but is not reinitialized when the function is called. This inside a function static variable retains its value during various calls.
void func(void);
static count=10; /* Global variable - static is the default */
main()
{
while (count--)
{
func();
}
}
void func( void )
{
static i = 5;
i++;
printf("i is %d and count is %d ", i, count);
}
This will produce following result
i is 6 and count is 9
i is 7 and count is 8
i is 8 and count is 7
i is 9 and count is 6
i is 10 and count is 5
i is 11 and count is 4
i is 12 and count is 3
i is 13 and count is 2
i is 14 and count is 1
i is 15 and count is 0
NOTE : Here keyword void means function does not return anything and it does not take any parameter.You can memories void as nothing. static variables are initialized to 0 automatically.Definition vs Declaration : Before proceeding, let us understand the difference between definition and declaration of a variable or function. Definition means where a variable or function is defined in reality and actual memory is allocated for variable or function. Declaration means just giving a reference of a variable and function.Through declaration we assure to the compiler that this variable or function has been defined somewhere else in the program and will be provided at the time of linking. In the above examples
char *func(void) has been put at thetop which is a declaration of this function where as this function has been defined below to main() function.
There is one more very important use for 'static'. Consider this bit of code.
char *func(void);
main()
{
char *Text1;
Text1 = func();
}
char *func(void)
{
char Text2[10]="martin";
return(Text2);
}
Now, 'func' returns a pointer to the memory location where 'text2' starts BUT text2 has a storage class of 'auto' and will disappear when we exit the function and could be overwritten but something else. The answer is to specify
static char Text[10]="martin";
The storage assigned to 'text2' will remain reserved for the duration if the program.
Extern - Storage Class - Extern is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern' the variable cannot be initalized as all it does is point the variable name at a storage location that has been previously defined.When you have multiple files and you define a global variable or function which will be used in other files also, then extern will be used in another file to give reference of defined variable or function. Just for understanding extern is used to decalre a global variable or function in another files.
File 1: main.c
int count=5;
main()
{
write_extern();
}
File 2: write.c
void write_extern(void);
extern int count;
void write_extern(void)
{
printf("count is %i ", count);
}
Here extern keyword is being used to declare count in another file.Now compile these two files as follows
gcc main.c write.c -o write. This fill produce write program which can be executed to produce result.Count in 'main.c' will have a value of 5. If main.c changes the value of count - write.c will see the new value.Global resides in data memory.
functions within a C Program..Two types of storage classes are there in c as explained below
1. Auto
2. Static
Storage class specifiers are
1. Auto
2. Static
3. Extern
4. Register
auto is the default storage class for all 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.
Variable Function Structure Pointer in C
Programming - C Viva Questions
Function in Embedded C
C Programming Basic Questions
Array in Embedded C
Arrays And Pointer in C
Register - Storage Class-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).Fast access but limited.Reside in CPU registers details.
{
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.
Static - Storage Class-static is the default storage class for global variables. The two variables below (count and road) both have a static storage class.
Static has three means
static variable
static global
static function
resides in data memory and BSS
static int Count;
int Road;
{
printf("%d ", Road);
}
Static variables can be 'seen' within all functions in this source file. At link time, the static variables defined here will not be seen by the object modules that are brought in.Static can also be defined within a function. If this is done the variable is initialized at run time but is not reinitialized when the function is called. This inside a function static variable retains its value during various calls.
void func(void);
static count=10; /* Global variable - static is the default */
main()
{
while (count--)
{
func();
}
}
void func( void )
{
static i = 5;
i++;
printf("i is %d and count is %d ", i, count);
}
This will produce following result
i is 6 and count is 9
i is 7 and count is 8
i is 8 and count is 7
i is 9 and count is 6
i is 10 and count is 5
i is 11 and count is 4
i is 12 and count is 3
i is 13 and count is 2
i is 14 and count is 1
i is 15 and count is 0
NOTE : Here keyword void means function does not return anything and it does not take any parameter.You can memories void as nothing. static variables are initialized to 0 automatically.Definition vs Declaration : Before proceeding, let us understand the difference between definition and declaration of a variable or function. Definition means where a variable or function is defined in reality and actual memory is allocated for variable or function. Declaration means just giving a reference of a variable and function.Through declaration we assure to the compiler that this variable or function has been defined somewhere else in the program and will be provided at the time of linking. In the above examples
char *func(void) has been put at thetop which is a declaration of this function where as this function has been defined below to main() function.
There is one more very important use for 'static'. Consider this bit of code.
char *func(void);
main()
{
char *Text1;
Text1 = func();
}
char *func(void)
{
char Text2[10]="martin";
return(Text2);
}
Now, 'func' returns a pointer to the memory location where 'text2' starts BUT text2 has a storage class of 'auto' and will disappear when we exit the function and could be overwritten but something else. The answer is to specify
static char Text[10]="martin";
The storage assigned to 'text2' will remain reserved for the duration if the program.
Extern - Storage Class - Extern is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern' the variable cannot be initalized as all it does is point the variable name at a storage location that has been previously defined.When you have multiple files and you define a global variable or function which will be used in other files also, then extern will be used in another file to give reference of defined variable or function. Just for understanding extern is used to decalre a global variable or function in another files.
File 1: main.c
int count=5;
main()
{
write_extern();
}
File 2: write.c
void write_extern(void);
extern int count;
void write_extern(void)
{
printf("count is %i ", count);
}
Here extern keyword is being used to declare count in another file.Now compile these two files as follows
gcc main.c write.c -o write. This fill produce write program which can be executed to produce result.Count in 'main.c' will have a value of 5. If main.c changes the value of count - write.c will see the new value.Global resides in data memory.
Very nicely explained.
ReplyDeleteThank you for your encouraging words.Keep visiting our blog for more updated contents.
DeleteAgain thumbs up from me.Please post tutorials for Array giving details about programming and use.If possible please provide video tutorials.Thank you in advance.
ReplyDeleteThanks +Echelon Tech.We will do our best to provide good contents to our readers.
Delete