Storage Classes in C

As a programmer, the complete knowledge about C storage classes in most important, without understanding of C storage it is impossible to write a good C program.

  • In C programming when we declare a variable, the C storage classes are used to define where memory will allocated, default value, scope and life time of the variables.
  • The knowledge about above four things is must to write efficient C programs.

Points about C storage

  1. It is used to define where memory will be allocated for the variables.
  2. It defines that, what will be default value of variable.
  3. It tells the scope of variable.
  4. It tells about life time of variables.

Based on memory allocations, default values, scope and life time of variables, C storage classes are categorised in 4 categories.

  1. Automatic Variables
  2. Register Variables
  3. Static Variables
  4. External Variables

Automatic Variables: auto

A variable which is declared inside a function without any storage class specification is by default an automatic variable. A variable declared inside a function is called as local variable and by default all local variables are automatic variables.

To define a variable as automatic variable we can use auto keyword in declaration of variable before data-type of variable. Let’s see memory allocation, default value, life-time and scope of auto variable.

  • Keyword: auto
  • Memory Allocation: RAM
  • Default Value: Garbage value
  • Scope: The variable can be access within the block or method in which it is declared.
  • Life Time: Life time of this kind variable ends when the block or method execution ends in which the variable is declared.

Output

Register Variables: register

register storage classes in c

The above image shows the memory allocation for source code file and variables. Source code file are stored in secondary storage like hard disk, flash drive etc. In run time local variable gets memory allocated in RAM. There are register between processor and ram for transferring value of variables. Register also can hold variable value but the storage size of registers is very less, so we can hold a very little value for a little time.

When we run a program first off all source code loaded in RAM and then block of code called by processor for processing. After processing the block of code processor returns the output to RAM and RAM displays the output. There are too many data transfer between Processor and RAM because processor can hold a very little data in registers.

register

A variable declared inside a function are method with register keyword are called as Register Variables. There is only one difference between register variable and local variable that is, in local variable memory allocated in RAM while in case of Register variable memory gets allocated in processor’s register. Register variables are much faster than auto variables.

  • Keyword: register
  • Memory Allocation: CPU Register
  • Default Value: Garbage value
  • Scope: The variable can be access within the block or method in which it is declared.
  • Life Time: Life time of this kind variable ends when the block or method execution ends in which the variable is declared.

Output

Static Variable

“Static variables are used to tell the compiler to persist/save the variable value until the ends of program. Unlike local variable, static variable cannot be assign again if it has assigned already.”

Static variables are destroyed only after complete execution of program. A static variable can be accessed only within the scope in which it is defined, but we can also mark a static variable as internal or external. The scope of internal static variable is within the function in which it is declared and the scope of external static variables is restricted to the scope of file in which it is defined.

c static storage classes

img 1.2 : block structure of main memory.

Static variables get stored in data block section of main memory. In general data block contains static variable and global variables.The four points of static variables are listed below.

  • Keyword: static
  • Memory Allocation: In data block of RAM
  • Default Value: 0
  • Scope: The variable can be access within the block or method in which it is declared.
  • Life Time: Life time of this kind variable ends after complete execution of programs.

Output

External or Global Variables

The variable which is declared out of any function or block is called as global variable. Global variable can be access anywhere in the program and can be modified by any operation in the program. The life time of these kind of variable is till end of program. The global variable get memory allocated in data segment of main memory (RAM ).

  • Keyword:
  • Memory Allocation: In data block of RAM
  • Default Value: 0
  • Scope: The variable can be access every where in the program.
  • Life Time: Life time of this kind variable ends after complete execution of programs.

Output

The extern keyword

The extern keyword used to tell compiler the variable is declared somewhere else, In this case compiler does not allocate the memory for the variable which is marked as extern. The extern keyword is also used to declare global variable. If we mark any variable as extern then it behaves as global variable.

  • Keyword : etern
  • Memory Allocation : In data block of RAM
  • Default Value : 0
  • Scope : The variable can be access every where in the program
  • Life Time : Life time of this kind variable ends after complete execution of programs.

program1.c

In above program1.c count is global variable which can be used in other program2.c by using extern keyword

program2.c

Output

Email Us: advertise@gdatamart.com

Donate Us: Support to GDATAMART

© 2023 GDATAMART.COM (All Rights Reserved)