Wednesday, October 13, 2010

C Storage Classes With Examples

,

Storage Class refers to the persistence of a variable (life time) and its scope within the program, that is, the portion of the program over which the variable is recognized.

The following types of storage-class specifications in C Language.

  • global
  • automatic or local
  • static
  • extern

The visibility of a variable determines how much of the rest of the program can access that variable. You can arrange that a variable is visible only within one part of one function, or in one function, or in one source file, or anywhere in the program.


Global Variables : A variable declared outside of any function is a global variable, and it is potentially visible anywhere within the program.

int sum=0; /* Declare global variables outside */

main()
{
 
}

Automatic or local variables : A variable declared within the braces {} of a function is visible only within that function; variables declared within functions are called local variables. You can use the keyword auto to declare automatic variables, but, however it is optional.

#include<stdio.h>

main()
{
 auto int n; /* Here the keyword auto is optional */
}

Static Variables : Static variables are defined within individual functions and therefore have the same scope as automatic variables, i.e. they are local to the functions in which they are declared. Unlike automatic variables, however, static variables retain their values throughout the life of the program.

static int count;

main()
{

}

External Variables : 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.

0 comments to “C Storage Classes With Examples”

Post a Comment