C Programming/Storage Class: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
(Created page with "A storage class defines the scope (visibility) and life-time of variables and/or functions within a '''C Program'''. They precede the type that they modify. We have four diffe...")
 
No edit summary
 
Line 5: Line 5:
# static
# static
# extern
# extern
__TOC__


== The auto Storage Class ==
== The auto Storage Class ==
Line 11: Line 14:
<syntaxhighlight lang="c">
<syntaxhighlight lang="c">
{
{
  int mount;
    int mount;
  auto int month;
    auto int month;
}
}
</syntaxhighlight>
</syntaxhighlight>
Line 21: Line 24:
<syntaxhighlight lang="c">
<syntaxhighlight lang="c">
{
{
  register int  miles;
    register int  miles;
}
}
</syntaxhighlight>
</syntaxhighlight>
Line 55: Line 58:
     i++;
     i++;
     printf("i is %d and count is %d\n", i, count);
     printf("i is %d and count is %d\n", i, count);
}
</syntaxhighlight>
== The extern Storage Class ==
The extern storage class 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 however, it points the variable name at a storage location that has been previously defined.
<syntaxhighlight lang="c">
//main.c
#include <stdio.h>
int count ;
extern void write_extern();
int main()
{
    count = 5;
    write_extern();
    return 0;
}
</syntaxhighlight>
<syntaxhighlight lang="c">
//support.c
#include <stdio.h>
extern int count;
void write_extern(void)
{
    printf("Count is %d\n", count);
}
}
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 05:26, 5 February 2018

A storage class defines the scope (visibility) and life-time of variables and/or functions within a C Program. They precede the type that they modify. We have four different storage classes in a C program :

  1. auto
  2. register
  3. static
  4. extern


The auto Storage Class

The auto storage class is the default storage class for all local variables.

{
    int mount;
    auto int month;
}

The register Storage Class

The register storage class 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 can't have the unary '&' operator applied to it (as it does not have a memory location).

{
    register int  miles;
}

The static Storage Class

The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope. Therefore, making local variables static allows them to maintain their values between function calls.

The static modifier may also be applied to global variables. When this is done, it causes that variable's scope to be restricted to the file in which it is declared.

In C programming, when static is used on a global variable, it causes only one copy of that member to be shared by all the objects of its class.

//main.c
#include <stdio.h>

void func(void);

//global variable
static int count = 5;

int main()
{
    while(count--){
        func();
    }
    return 0;
}

void func( void )
{
    //local variable
    static int i = 5;
    i++;
    printf("i is %d and count is %d\n", i, count);
}

The extern Storage Class

The extern storage class 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 however, it points the variable name at a storage location that has been previously defined.

//main.c
#include <stdio.h>

int count ;
extern void write_extern();

int main()
{
    count = 5;
    write_extern();
    return 0;
}
//support.c
#include <stdio.h>

extern int count;

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