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

Related Posts:

  • C language 'C' is a well known programming language which was developed by Dennis  Ritchie  in 1972 in  AT&T Bell Labs. This programming language was influenced by B , BCPL ,  ALGOL, and some other programming… Read More
  • Decision Making Structure In C Decision making structures allows the programmer to specify one or more conditions to be evaluated or tested by the compiler or , along with a statement or set of statements to be executed if the condition is  … Read More
  • Scope Rules In C A scope in any program is a that region of the program where a defined variable can have its existence and beyond that variable can't be accessed. The three places where variables can be declared in C programming l… Read More
  • Operators in C An operator is a symbol that instruct  the compiler to perform specific mathematical or logical operation. C language  has following type of operators: C Programming •  Arithmetic Operators •  … Read More
  • 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 Pr… Read More

0 comments:

Post a Comment