Sunday, March 11, 2012

static storage class in c


A storage class defines the scope (visibility) and life time of variables and/or functions within a C Program. There are following storage classes which can be used in a C Program

auto
register
static
extern

auto - Storage Class
auto is the default storage class for all local variables.
    
{
int nDay;
     auto int nMonth;
}

The example above defines two variables with the same storage class. auto can only be used within functions, i.e. local variables.

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).

{
     register int  nMiles;
}

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 int nStaticCount;
int nRoadNumbers;
{
printf("%d\n", nRoadNumbers);
}

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 initalised at run time but is not reinitalized when the function is called. This inside a function static variable retains its value during vairous calls.

void func(void);
static int nCount = 10; /* Global variable - static is the default */
void main()
{
while (nCount --)
{
func();
     }
}
  
void func( void )
{
static nI = 5;
nI++;
printf("nI is %d and nCount is %d\n", nI, nCount);
}


/*
 * This will produce following result
 *
   nI is 6  and nCount is 9
   nI is 7  and nCount is 8
   nI is 8  and nCount is 7
   nI is 9  and nCount is 6
   nI is 10 and nCount is 5
   nI is 11 and nCount is 4
   nI is 12 and nCount is 3
   nI is 13 and nCount is 2
   nI is 14 and nCount is 1
   nI is 15 and nCount is 0
 */

example: static variable c++ code

NOTE : Here keyword void means function does not return anything and it does not take any parameter. You can memorize 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.

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 initialized 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 declare a global variable or function in another files.

/*
 * File 1: main.c
 * extern storage class specification
 */

#include <stdio.h>

int nCount = 5;

void main()
{
write_extern();
}

/*
 * File 2: write.c
 * extern storage class specification
 */

#include <stdio.h>

void write_extern(void);
extern int nCount;

void write_extern(void)
{
printf("nCount is %d\n", nCount);
}

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.

nCount in 'main.c' will have a value of 5. If main.c changes the value of nCount - write.c will see the new value

No comments:

Post a Comment