C Language Introduction

Storage Classes in C

Storage Classes in C
Storage Classes in C

Storage Classes in C

Storage Class Specifiers

1) Scope

Scope of a variable is that portion of the program in which that identifier can be used. There are two types of scopes –

Block Scope

When a variable is declared inside a block then it is called block scope. It’s scope from the point of its declaration and ends at the end of the block.

All automatic variables have the block scope.

File Scope

Variables having file scope are defined outside of all blocks. Their scope starts from the point of their declaration to the end of the source file.

All global variables have the file scope.

2) Visibility

The visibility of a variable is that portion of the program source code from which legal access can be made to the variables. Both scope and visibility are tightly close together. Scope of a variable is remains fixed but the visibility can get hidden.

Storage Classes in C
Storage Classes in C

3) Lifetime

The lifetime of a variable is the period of time during which that variable physically exists and retains a particular value. Variable can have three types of lifetimes-

  • Static Lifetime

Variables having static lifetime are allocated memory as soon as execution process begins.This storage allocation lasts untill the program terminates. Static lifetime variables reside in the fixed data area of the memory. All variables with file scope have static lifetime. Variables can also be given static lifetime by using static or extern storage class specifiers. Static lifetime variables are all set to zero if they are not explicitly initialized to some value.

  • Local Lifetime

Local lifetime variables are also called automatic variables and are created on the runtime stack of memory (or in CPU registers if specified as register variables) when the enclosing block or function is entered.   They are deallocated when that block or function terminates.

The variables having local lifetime must be initialized explicitly otherwise it will take garbage value.

  • Dynamic Lifetime

Dynamic lifetime objects are created and destroyed explicitly by the request of the programmer using specific function calls e.g. malloc() and free() during the program. They get the storage area allocated from a special reserved area called heap or free area.

4) Linkage

Linkage is the connection between the actual declaration and the variable. This process is done by linker. Linkage can be of three types

  • No linkage

When the name of the variable is resolved at the context itself by the compiler, then there is no linkage is involved.

All local variables have not linkage at all.

  • Internal linkage

When the name to linked is in the current file itself then it is called internal linkage.

all variables having global scope have internal linkage.

  • External linkage

When the definition for the variable declared in the current file can be available anywhere outside the current file then it is called external linkage.

These variables are declared with ‘extern’ keyword.

Storage Classes

A storage class is used to define the scope, visibility, life-time and linkage of variables and/or functions within a C Program. They precede the type that they modify. We have five different storage classes in a C program −

  • auto
  • register
  • static
  • extern
  • typedef

The auto Storage Class

All the local variables have default automatic storage class also called as auto.

{

int month;

auto int year;

}

The two variables month and year have the same storage class. ‘auto’ can optionally be used within functions, i.e., local variables.

Register Storage Class

The register storage class variables are stored in a CPU registers instead of RAM. Which means that the variable has a maximum size equal to the register size (usually one word) and can’t have the address operator ‘&’  applied to it (because it does not have a memory location).

{

register int  miles;

}

The register storage class should only be used for variables that require quick access such as counters.

The static Storage Class

If a variable is declared with static storage class specifier then-

  • The storage of the variable is allocated from the data area of the memory
  • The variable is not available outside the function or file in which it is declared.
  • If the variable is not initialized explicitly then it takes default initial value as zero.
  • It receives the initial value before the execution of main function, and retains it until the program stops execution.
#include <stdio.h>
void func(void);
static int count = 5; /* global variable */
main()
{
   while(count--) {
      func();
   }
   return 0;
}
void func( void )
{
   static int a = 5;
   a+;
   printf("a is %d and count is %d\n", a, count);
}

The following result would appear on compiling and executing the above program −

a is 6 and count is 4

a is 7 and count is 3

a is 8 and count is 2

a is 9 and count is 1

a is 10 and count is 0

The extern Storage Class

The extern storage class is used to refer to a global variable that has been defined else where in some other program. When extern storage class is used for a variable , the variable cannot be initialized however, it points the variable name at a storage location that has been previously defined.

The extern storage specifier is used when there are two or more programs share the same global variables or functions.

First File: mainfile.c

#include <stdio.h>
#include"support.c"
int count ;
extern void write_extern();
main()
{
   count = 5;
   write_extern();
}

Second File: support.c

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

In this example, extern is being used to declare count in the second file, which has been defined in the first file, mainfile.c.  To compile this program we must make forward declaration in mainfile.c by including support.c  −

on compiling  mainfile.c file. It will produce the following result −

count is 5

Typedef

The typedef storage class is different from other storage classes in that it is used tocreate user defined data types.

Syntax for typedef is

typedef type synonym

typedef char * string;

typedef unsigned char byte;

Official TurboC version can be downloaded from here

Similar Posts :

C Language Introduction         Elements of C Language              Variables in C

You May Also Like :

Reverse Charge Mechanism in Tally.ERP9            JavaScript String and Date Objects

 

What are storage classes in c?





Storage Classes
A storage class is used to define the scope, visibility, life-time and linkage of variables and/or functions within a C Program. They precede the type that they modify. We have five different storage classes in a C program −
auto
register
static
extern
typedef

What is Scope of a variable

Scope of a variable is that portion of the program in which that identifier can be used. It is of two types block scope and file scope.


What is Visibility of an identifier in c?



The visibility of a variable is that portion of the program source code from which legal access can be made to the variables. Both scope and visibility are tightly close together. Scope of a variable is remains fixed but the visibility can get hidden.
5 1 vote
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Veerta Thakur
Veerta Thakur
1 year ago

Helpful topic

Please Subscribe to Stay Connected

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

DigitalSanjiv will use the information you provide on this form to be in touch with you and to provide updates and marketing.
Share via
Copy link
Powered by Social Snap
%d bloggers like this: