Storage Classes

5
Swetha, ATRI Page 1 STORAGE CLASSES: In ‘C’ the variables are declared by the data type they can hold. The name of a variable is associated with a memory location within the computer where the value assigned to the variable is stored in the form of bits. During the execution of the program, these variables may be stored in the registers of the CPU or the primary memory of the computer. To indicate where the variables would be stored, how long they would exist, what would be the region of existence, and what would be the default values, C provides four storage class specifiers that can be used along with the data type specifiers in the declaration statement of a variable. 1) Automatic storage class 2) Register storage class 3) Static storage class 4) External storage class The storage class specifier precedes the declaration statement for a variable. The general form of the variable declaration statement that includes the storage class specifier is given as follows <storage class specifier> <data type> <variable name> A variables Storage class tells us: 1) Where the variable is stored. 2) Initial value of the variable. 3) Scope of the variable. Scope specifies the part of the program which a variable is accessed. (i.e., in which functions the value of the variable would be available). 4) Life of the variable. (i.e., how long the variable exist in the memory) The scope of a variable determines over what part of a program a variable is actually available for use; i.e., active in the program. The life time refers to the period during which a variable retains a given value during the execution of the program; i.e, alive, known as longevity. 1. AUTOMATIC STORAGE CLASS: By default, all variables declared within the body of any function are automatic. These variables are created when a function is called and destroyed automatically when the function is exited. Automatic variables are private to that function. Because of this property, automatic variables are also referred to as local or internal variables. The keyword auto is used in the declaration of a variable to explicitly specify its storage class.The format for specifying automatic variables are auto datatype var1,var2..varn; In this automatic storage class, Variable is stored in memory. Default value is garbage value. Scope is local to the block in which it is defined. Life is, with in the block in which the variable is defined Example 1: Example 3: main() main() { { auto int i=10; auto int a,b; printf(“%d”,i); a=10; } fun1();

description

This gives u an over view of storage classes in c-language

Transcript of Storage Classes

Page 1: Storage Classes

Swetha, ATRI Page 1

STORAGE CLASSES: In ‘C’ the variables are declared by the data type they can hold. The name of a variable is associated with a memory location within the computer where

the value assigned to the variable is stored in the form of bits. During the execution of the program, these variables may be stored in the registers of the CPU or the primary memory of the computer. To indicate where the variables would be stored, how long

they would exist, what would be the region of existence, and what would be the default

values, C provides four storage class specifiers that can be used along with the data type specifiers in the declaration statement of a variable.

1) Automatic storage class 2) Register storage class 3) Static storage class

4) External storage class

The storage class specifier precedes the declaration statement for a variable. The general form of the variable declaration statement that includes the storage class

specifier is given as follows <storage class specifier> <data type> <variable name>

A variables Storage class tells us:

1) Where the variable is stored. 2) Initial value of the variable.

3) Scope of the variable. Scope specifies the part of the program which a variable is accessed. (i.e., in which functions the value of the variable would be available).

4) Life of the variable. (i.e., how long the variable exist in the memory)

The scope of a variable determines over what part of a program a variable is

actually available for use; i.e., active in the program. The life time refers to the period during which a variable retains a given value during the execution of the program; i.e, alive, known as longevity.

1. AUTOMATIC STORAGE CLASS: By default, all variables declared within the

body of any function are automatic. These variables are created when a function is

called and destroyed automatically when the function is exited. Automatic variables are private to that function. Because of this property, automatic variables are also referred to as local or internal variables.

The keyword auto is used in the declaration of a variable to explicitly specify its storage class.The format for specifying automatic variables are

auto datatype var1,var2..varn;

In this automatic storage class, Variable is stored in memory.

Default value is garbage value. Scope is local to the block in which it is defined.

Life is, with in the block in which the variable is defined

Example 1: Example 3: main() main()

{ { auto int i=10; auto int a,b;

printf(“%d”,i); a=10;

} fun1();

Page 2: Storage Classes

Swetha, ATRI Page 2

Output: printf(“%d”,a); 10 }

Example 2: void fun1() main() { { int a=15;

auto int i; printf(“%d”,a);

printf(“%d”,i); } } output: a=15

Output: a=10 1002

In example 1, i value is initialised to 10.So,output is 10.

In example 2, i value is not initialised.So,compiler reads i value is a garbage value.

REGISTER STORAGE CLASS: The variables stored in registers of the CPU are

accessed in much lesser time than those stored in primary memory. So, frequently accessed variables are stored in registers for faster execution of the program. A variable can be declared as a register variable by using the keyword register and it tells

the compiler that the variable should be kept in one of the CPU registers, instead of

keeping in the memory where normal variables are stored. The format for specifying the register variables are

Register datatype var1,var2,…varn; Example :

main() {

register int a,b,c;

a=10;b=20; c=a+b; printf(“%d”,c);

} In register storage class,

Storage -- CPU registers.

Default value-- garbage value. Scope -- local to the block in which the variable is defined Lifetime – till control remains within the block in which the variable is defined.

We can not use register storage class for all types of variables.

STATIC STORAGE CLASS: A variable can be declared as a static variable by using the

keyword static and as the name suggests, the value of static variable persists until the end of the program. The format for specifying the static variables are Static datatype var1,var2,..varn;

Two kinds of variables are allowed to be specified as static variables 1. Local variables 2. Global variables

The local variables are also referred to as internal variables. Internal variables are declared inside a function. They are identical to automatic variables in the function scope except that they retain the values throughout the program

For example, /* program to count no of calls made to a function*/

main()

{

Page 3: Storage Classes

Swetha, ATRI Page 3

int i; for(i=1;i<=3;i++)

stat(); } stat()

{

Static int x=0; x=x+1;

printf(“x=%d\n”,x); } Output:

x=1

x=2 x=3

A static variable is initialized only once, when the program is compiled, it is never initialized again. During the first call of stat x is incremented to 1.because x is static this value persists and therefore, the next call adds another 1 to giving it a value of 2.

The value of x becomes 3 when the third call is made.

External static variables are declared outside all functions including the main() function.

They are declared global variables but are declared with the keyword static. The difference between the static external variable and a simple external variable is that

static external variable is available only within the file in which it is defined while the external variable can be accessed by other files

The important things regarding static variables is as follows

Storage: main memory. Default value: zero. Keyword: static

Scope; local to the block in which it is defined Life time: throughout the program(value of the variable persists between different

function calls)

NOTE: Static and auto storage classes both are different in the case of life. Remaining all are

same

EXTERNAL STORAGE CLASS VARIABLE: Variables that are both alive an active

throughout the program are called external variables. These are also known as global variables. Unlike local variables, global variables can be accessed by any function in the program. External variables are declared outside a function. One a variable has been

declared as global, any function can use it and change its value. Then the functions executing the subsequently can reference only that new value.

int num;

float length=7.5; main() {

… …

}

func1()

Page 4: Storage Classes

Swetha, ATRI Page 4

{ ….

} func2() {

….

} The variables num and length are available for use in all the 3 functions. In case a

local variable and a global variable have the same name, the local variable will have the higher precedence over the global one in the function where it is declared. int count;

main()

{ count=10;

…. }

function1()

{

int count=0; …..

count=count+1; }

When the function references the variable count, it will be referencing only its local variable, not the global one. The value of count in main will not be affected.

� One another aspect of global variable is that it is available only from the point of

declaration to the end of the program. Let us consider an example, main() {

y=5; ….

….

} int y; func1()

{ y=y+1;

}

In the main function y is not visible or available that’s why the compiler gives an error message. This problem can be solved by declaring the variable with the storage class extern. In order to be able to use an externally declared variable inside a function, such

a variable may be explicitly redeclared inside the function with extern qualifier. extern int y;

ex:

main() { extern int y;

…. ….

}

func1()

Page 5: Storage Classes

Swetha, ATRI Page 5

{ extern int y;

} int y;

The extern keyword tells the ‘C’ compiler that the variable thus qualified is actually one

of the same names externally; this is the one that will be used in the function.

The important things regarding static variables is as follows Variable is stored in memory.

Default value is zero. Scope is entire program. Life is, from the point of declaration to till the end of the program.