Monday 1 October 2012

Storage classes


Storage classes :  A variable in c can have four stroage classes.


1.  Automatic variables
2.  External variables
3.  Static variables
4.  Register variables.

1. Automatic variables :  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 also referred to as local variables.The general declaration of automatic variables

auto data type  data item;
Ex :   
main( )
{
int m=100;
function2( )
printf("%d\n",m);
}
function1( )
{ int m=10;
printf("%d\n",m);
}
function2( )
{
int m=100;
function1( );
printf("%d\n",m);
                                    }

output :                                   10
                                                100     
                                                1000

  Here, two  subprograms function1 and function2 ,  m  is  an automatic  variabale  and it is declared at the beginning  of  each function.  After evaluation of the program m is initialized  to  10, 100,1000 in function1 ,funtction2 and main respectively.



2.  External  variables :  Variables that are both alive  and  active through the entire program are known as external variables.  They are  also  known as global variables. unlike  local  variables  , global  variables can be accessed by any function in the  program. External variables are declared outside a function.

Ex :                             int x;
main( )
{
X = 10;
printf("x=%d\n", x);
printf("x=%d\n",fun1( ));
printf("x=%d\n",fun2());
printf("x=%d\n",fun3());
                                    }
                                    fun 1 ( )
{
x=x+10;
return(x);
{
fun2()
                                    {
int x;
x = 1;
return(x);
}
fun3( )
{
x=x+10;
return(x);
}

Here  x is used in all functions,but expect in fun2( ),  in all  functions x is a global variable.  Once a variable  has  been declared as global , any function can use it and change its value.  Then subsequent functions can reference only the new value.Then after evaluation of the above program , the output will be
                                   
                                                X = 10
                                                X = 20
                                                X = 1
                                                X = 30

3.  Static  Variables :  The value of static variables having the  same value until the end of the program.  A variable can  be  declared static using the keyword "static".

A  static  variable may be either an internal  type  or  an external type, depending on the place or declaration.

Internal  static  variables  are those  which  are  declared inside  a  function.  The scope of the internal  static  variables extend upto the end of the function in which they are  defined. i.e.., the  internal  static  variables are similar  to  auto  variables. 

External static variables are those which are declared outside  of all functions and is available to all the functions on the program.  The  difference  between a static external variable and  a  simple external  varible  is that the static external  variable  available only within the file where it is defined while the simple external variable can be accessed by other files.

Ex :                             main()
{
int i;
for(i=1;i<=3;i++)
stat( );
stat( )
                                    {
static int x=0;
x=x+1;
printf("x=%d\n",x);
{
Output :
                        X = 1
                        X = 2 
                        X = 3

4. Register  Variables :  We can tell the compiler that a  variable should  be  kept  in one of the machine's  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.  Since only a few variables can be  placed in the register, it is important to carefully select the  variable for  this purpose .  C will automatically convert register  variables into nonregister variables once the limit is reached. This is done as follows

register int count;


Command  Line Arguments :  Command line arguments  are  parameters
supplied to a program when the program is invoked.  The parameters may represent a filename the program should process.  For  example if  we  want to execute a program to copy the contents of  a  file named X-file  to  another one named Y-file , then we  may  use  a command line like

C:\>PROGAM X-FILE Y-FILE

PROGRAM  is  the filename where the executable code  of  the program is stored.

Mainfunction can take two arguments called argc and  argv and the information contained in the command line is passed on  to the program through those arguments  when main is called up by the system.




The  variable argc is an argument counter that counts the  number of  arguments on the command line.  The arv is an argument  vector and  represents  an array of character pointer that point  to  the command  line arguments.  The size of this array will be equal  to
the  value  of argc. argc is three and argv is an array  of  three pointers to strings as shown below

argv[0]------>PROGRAM

argv[0]------>X_FILE
           
argv[2]------>Y_FILE

In  order  to access the command line  arguments  is  as follows


main(argc, argv)
int argc;
char *argv[];
{
            |
            |
            |
}

No comments:

Post a Comment