• No results found

Storage Classes

N/A
N/A
Protected

Academic year: 2020

Share "Storage Classes"

Copied!
5
0
0

Loading.... (view fulltext now)

Full text

(1)

1

B.Naresh Assistant Professor in B.V.R.I.C.E, Dept., of MCA.B.Naresh Assistant Professor in B.V.R.I.C.E, Dept., of MCA.B.Naresh Assistant Professor in B.V.R.I.C.E, Dept., of MCA.B.Naresh Assistant Professor in B.V.R.I.C.E, Dept., of MCA.

STORAGE CLASSES

The variables declared in C programs are totally different from other languages. We can use same variable names in the C program in separate blocks.

The area or block of the C program from where the variable can be accessed is known as the scope of the variable. The area or scope of the variable depends on its storage class i.e., where and how it is declared. There are four scope variables.

Function

File

Block

Function prototype.

The storage class of a variable tells to the compiler

The storage area of the variable.

The initial value of the variable if not initialized.

The scope of the variable.

Life of the variable i.e., how long the variable would be active in the program.

Any variable declared in c can have any one of the four storage classes.

Automatic variables.

External variables.

Static variables.

Register variables.

Automatic variables (auto):

Auto variables are defined inside a function. A variable declared inside the function without storage class name, by default is an auto variable. These functions are declared on the stack. The keyword auto can be used to declare the auto variables.

Example: auto int a;

For an auto variable,

The storage location is stack.

The default value is a garbage value.

The scope of the variable is local to block in which they are defined.

(2)

2

B.Naresh Assistant Professor in B.V.R.I.C.E, Dept., of MCA.B.Naresh Assistant Professor in B.V.R.I.C.E, Dept., of MCA.B.Naresh Assistant Professor in B.V.R.I.C.E, Dept., of MCA.B.Naresh Assistant Professor in B.V.R.I.C.E, Dept., of MCA. The following program illustrates the working of auto variables in C.

Aim: C-Program to illustrates the working of auto variables in C.

Program:

#include<stdio.h>

#include<conio.h>

void main()

{

int x=10;

clrscr();

printf("\nX=%d",x);

{

int x=20;

printf("\nX=%d",x);

}

printf("\nX=%d",x);

getch();

}

Output:

External Variables

The variables that are available to all the functions in the program i.e., from entire program they can be accessed are called an external variable or a global variable. External variables are declared outside of the function body. The keyboard extern can be used to declare an external variable.

Example: extern int a;

For an external variable,

The storage location is stack.

The default value is zero (0).

The scope of the variable is entire the program.

(3)

3

B.Naresh Assistant Professor in B.V.R.I.C.E, Dept., of MCA.B.Naresh Assistant Professor in B.V.R.I.C.E, Dept., of MCA.B.Naresh Assistant Professor in B.V.R.I.C.E, Dept., of MCA.B.Naresh Assistant Professor in B.V.R.I.C.E, Dept., of MCA. The following program illustrates the working of an external storage class.

Aim: To illustrates the working of an external variable in c.

Program:

#include<stdio.h> #include<conio.h> int x=10;

void main() {

clrscr(); f1(); f2();

printf("\n In main() x=%d",x); getch();

} f1() {

int x=20;

printf("\n In f1() x=%d",x); }

f2() {

printf("\n In f2() x=%d",x); }

Output:

Static Variables

If a variable is defined with the keyword static then it is called static storage class

variable. The initial value of static storage class variable is zero. We can have both the local

and global static variables.

The static is mainly used to maintain the previous value of the variable if the

programming is there or not. Generally static is used for counting the function how many

(4)

4

B.Naresh Assistant Professor in B.V.R.I.C.E, Dept., of MCA.B.Naresh Assistant Professor in B.V.R.I.C.E, Dept., of MCA.B.Naresh Assistant Professor in B.V.R.I.C.E, Dept., of MCA.B.Naresh Assistant Professor in B.V.R.I.C.E, Dept., of MCA. The following program illustrates the working of a static variable.

Aim: To write a C program that illustrates the working of a static variable in C.

Program:

#include<stdio.h> #include<conio.h> #include<process.h> void main()

{

void print(); clrscr();

for( ; ;) //Infinit loop {

print(); }

}

void print() {

static int m;//Default value is zero m++;

printf("\n m=%d",m); if(m>3)

{

getch(); exit(0); }

}

(5)

5

B.Naresh Assistant Professor in B.V.R.I.C.E, Dept., of MCA.B.Naresh Assistant Professor in B.V.R.I.C.E, Dept., of MCA.B.Naresh Assistant Professor in B.V.R.I.C.E, Dept., of MCA.B.Naresh Assistant Professor in B.V.R.I.C.E, Dept., of MCA. Register Variables

If a variable is declared with the keyword register then it is called as register storage

class variable. When a variable is defined under the register then the value of the register then

the value of the variable directly stored in CPU registers. As we know the processor fastly

access the registers, so that the register storage class variables are fastly accessed compare to

normal variables.

As the CPU registers are less in number we have to take less number of register

variables only. If we define more number of register variables and if there is no availability

then register storage class variables automatically convert into auto storage class variable by

the compiler.

Float is not suitable for register class variable .Initial value is garbage, scope and life

time is within the block.

The following program illustrates the working of register storage class.

Aim: To write a C program that illustrates the use of a register variable in C.

Program:

#include<stdio.h>

#include<conio.h>

void main()

{

register int a;

clrscr();

a=10;

printf("\n a=%d",a);

getch();

}

Output:

References

Related documents

Although we often think of stem cell therapy acting to regenerate tissue through cell replica- tion and then differentiation, growing evidence suggests that adult stem cells may

Theoretically, the result of this study aimed to measure the effect of using word wall technique toward students’ vocabulary mastery at eight graders of MTs

Berdasarkan hasil analisis deskriptif diketahui bahwa sistem perkandangan dalam Kelompok Tani Ternak Subur Makmur memiliki tingkat kebersihan lantai kandang yang buruk,

Similarly these normalized rank-1 CP matrices together with the normalized extremely bad matrices constitute the extreme points of ( 23 ).. We prove

The intervention working group [JIIECD intervention support phase], a committee of partners from the project and from the Institut national de santé publique du Québec (INSPQ), has

A mathematical model was used for estimating the solar radiation on a tilted surface, and to determine the optimum tilt angle and orientation (surface azimuth angle) for the solar

(a) [N]o citizen shall be denied the right to vote in any Federal, State, or local election because of his failure to comply with any test or device in any State with

The relative discrepancy on the major target sequences in different hosts infected by ToCMoV-[BA-Se1] might reflect different compositions of the silencing machinery in the two