Posts

Showing posts from March, 2013

Storage Classes

Storage Classes -          A variable always have data type(int,float etc) but they also have a storage class or we can say a variable always need data type as well as storage class. -          As we had seen many examples before, but we did not use storage classes there, because variables also have default storage classes. So if we do not mention storage class, compiler will assume default. -          In computer we use two types of memory 1.      Internal Memory  2.      CPU Registers. -                     It is the variable’s storage class that determines in which of these two locations the value is stored. What is use of storage class? -          To know about life of variable -          To know about scope of variable -          To store variables in memory or registers -          To know initial value of variable, if we did not assign default value. Types of Storage classes:      There are 4 types of storage class 1.       Automatic Storage

External Storage Class

Image
  External Storage Class: ·        Values stores in memory only. ·        Default value of external storage value is zero. ·        Variable scope is global. ·        Life of variable is as long as the program’s execution doesn’t come to end ·        External variables declared outside function, So that function may use it. These variables are available to all functions. #include<stdio.h> void useFile(); /* this variable is external variable, its default value will be zero */ char* fileName="test.txt";     main( ) {        printf("File main function  - %s\n",fileName);              useFile(); } void useFile() {        printf("File useFile function  - %s\n\n",fileName); } In this program, we define an external variable (fileName) which is a char pointer. This variable is available in all function. We use filename in both function and we can see output in attached image. Example 2: #include<stdio.h> in

Static Storage Class

Image
  Static Storage class: ·        Variables stored in memory. ·        Default value of static variables is zero. ·        Scope of variables is local to block in which it is defined ·        Life of variables is till end of program. Its value initialized only one time and exists till complete execution. ·        auto and static both variables are local to block in which they defined, Difference only is that static variables don’t disappear when function is no longer alive. It remains in memory, so when control comes back to function , static values will be having same value as before. ·        As values of static variables kept in memory when variables are not active, So its wastage of memory. So we should use static variables only when you really need. Example: #include<stdio.h> void test();    // Function Prototype main( ) {       test();       test();       test(); } void test() {    static int i=1;   // Static variable    i=i+1;    printf("Function c

Register Variable Class

    Register Variable class ·        It stores variables in Register memory ·        Default value of register variables is garbage value. ·        Scope of register variables is also local to block. ·        Life of variables is also remains till control remains in block. ·        Values stored in register access more faster than values stored in memory ·        So if a variable is used at many places in program, it’s better to declare its storage class as register. So it will increase the performance of program. ·        CPU registers are limited. So during execution of program, value may be stored in register , because register may be busy doing another CPU tasks. At that time those variables will also works as auto variables.   ·        Keyword - register main() {       register int i;                    printf(“%d”,i); }

Automatic storage class

Image
Automatic Storage Class: ·        It stores variable in memory ·        Initial value of automatic class variables is garbage values. ·        Variable scope is local to block in which it is defined. ·        Life of variable will be only till control remains in block. ·        Keyword - auto Example:                   This example shows how to define a auto variable.           #include<stdio.h>      main( )      {           auto int i, j ;           printf ( "\n%d %d\n", i, j ) ; } In this example, we did not initialize I and j variable, so it displays garbage values after execution.    Example 2 : this example will illustrate scope of auto variable. #include<stdio.h> main( ) {           auto int test=100;           {                     auto int test=200;                     {                               auto int test=300;                              printf("%d   ",test);                      }