Sunday, 24 November 2013

Storage Classes in c

A storage class in C defines the scope (visibility) and life-time of a variables and/or a functions .These specifiers precede the type that they modify. There are some storage classes, which are  used in a C Program
 C Programming
C Programming


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

The auto Storage Class:

The auto storage class is the default class for local variables in a c program.

{
   int m;
   auto int mon;
}
The example defines two variables with the same storage class and auto can only be used within a functions, i.e., only for local variables.

The register Storage Class:

The register storage class is  used to define  variables(local) that supposed to be stored in a register instead of RAM. The variable has a maximum size equal to the register size (usually one word) and can't have the unary '&' operator applied to it ( because  it does not have a memory location).

{
   register int  mile;
}
The register should only be used for those variables that require quick access such as counters. Defining 'register' does not mean that the variable will be stored in a register rather it  MIGHT be stored in a register depending on hardware and implementation restrictions.

The static Storage Class:

The static storage class commands the compiler to keep a local variable alive during the life-time of the program instead of creating and destroying it each time when it comes into and goes out of scope.Static allows a variable to maintain their values between function calls.

The static modifier can also be applied to global variables. The variable's scope to be restricted to the file in which it is declared.

#include <stdio.h>

/* function declaration */
void func(void);

static int count = 5; /* global variable */

main()
{
   while(count--)
   {
      func();
   }
   return 0;
}
/* function definition */
void func( void )
{
   static int i = 5; /* local static variable */
   i++;

   printf("i is %d and count is %d\n", i, count);
}
Output Is:

i is 6 and count is 4
i is 7 and count is 3
i is 8 and count is 2
i is 9 and count is 1
i is 10 and count is 0

The extern Storage Class:

The extern storage class is used to give a reference of a  variable(global) that is visible to all  program files. When you use 'extern' keyword, the variable can't be initialized and it point the variable name at a storage location that has been previously defined.

A extern storage class is more efficient  when you are using multiple files .You define a variable in one of the files which will be available at the time of linking of the program. You can use extern keyword to declare a variable at any place.

First File: mainfile.c

#include <stdio.h>

int count ;
extern void write_extern();

main()
{
   c = 5;
   write_extern();
}
Second File: supportfile.c

#include <stdio.h>

extern int count;

void write_extern(void)
{
   printf("count is %d\n", c)
}
The extern keyword is being used to declare c in the second file and its definition is  in the mainfile.c.
 compile these two files as shown below:

 $gcc main.c support.c

 following is the result:
 5
C - Overview
C - Basic Syntax
C - Data Types
C - Variables
C - Constants
C - Storage Classes
C - Operator
C - Decision Making
C - Loop
C - Functions
C - Scope Rules
C - Arrays
C - Pointers
C - Strings
C - Structures
C - Unions
C - Bit Fields
C - Typedef
C - Input & Output
C - File I/O
C - Preprocessors
C - Header Files
C - Type Casting
C - Error Handling
C - Recursion
C - Variable Arguments
C - Memory Management
C - Command Line Arguments

0 comments:

Post a Comment