0% found this document useful (0 votes)
10 views11 pages

Unit 6 Part3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views11 pages

Unit 6 Part3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Programming Principle Algorithm(P.P.

A)
Unit -6
Part3
Topics Covered in UNIT 6

➢ Storage classes
1. Type of storage classes
2. Example
Functions
Storage Classes: Storage classes define the visibility (scope) and the lifetime of any
function/ variable within a C program.
Type of storage classes
1. Automatic Storage Class
2. External Storage Class
3. Static Storage Class
4. Register Storage Class
Automatic Storage Class

Automatic Storage Class: It is also known as the auto storage class, and it acts as
the default storage class for all the variables that are local in nature.

Example:

int week; auto int week;

Features of Automatic storage class:

1. It allocates the memory during the run time.


2. Scope of variable is local or limited within function or block.
3. It contains default values a garbage value.
4. Memory gets free when the block or program ended.
External Storage Class

External Storage Class: It is used for giving a reference of any global variable which
is visible to all the files present in a program. When using the extern storage class, we
cannot initialize the variable.

Example:

extern int week;

Features of External storage class:


1. When we declare a variable as external, the compiler will start searching for
that variable for initialization somewhere in the available program. It might be
static or extern
2. An external integral type default initial value is going to be 0, or else it is null.
3. The variables that are declared as extern have no allocation of memory
External Storage Class

Example: #include <stdio.h>


int main()

extern int x; // The compiler will start searching here if a variable x has
been defined and initialized in the program somewhere or not.

printf(“%d”,x);

int x = 20;
Static Storage Class

Static Storage Class: This type of storage class gives an instruction to a compiler to
keep the given local variable around during the program’s lifetime- instead of creating
it and then destroying it every time it comes into a scope and goes out of it.

Example:

static int week;

Features of Static storage class:


1. The static local variables are only visible to the block or the function in which
we have defined them.
2. The initial value of a static integral variable, by default, is 0. Else, it is null.
3. The visibility of any static global variable stays limited to that file in which we
have declared it.
Static Storage Class

Example: #include<stdio.h>

void sum() {

static int x = 20; static int y = 34;

printf(“%d %d \n”,x,y);

X++; y++; }

void main() {

int a;

for(a = 0; a< 3; a++) {

sum();

} }
Register Storage Class

Register Storage Class: We use the register storage class for defining the local
variables that must be stored in any register, and not in a RAM.

Example:

register int week;

Features of Register storage class:


1. The default initial value of any given register local value will always be 0.
2. Its access time is comparatively much faster than that of the automatic
variables.
3. Those variables that we define as the register have their memory allocation into
the CPU registers.
Register Storage Class

Example: #include <stdio.h>


int main()

register int x; // A variable x has memory allocation in the CPU


register. Here, the initial value of x, by default, is 0.

printf(“%d”,x);

You might also like