Storage Class in C INTRODUCTION TYPES Introduction Storage





























- Slides: 29

Storage Class in C ØINTRODUCTION ØTYPES

Introduction �Storage class explain the behavior of the variable in terms of scope and lifetime, it also determine the initial of the variable. �Scope of the variable is the region over which the variable is visible or valid. �Life time of the variable is the time during which memory is associated with the variable. �Initial value is the value assigned to the variable implicitly if no value is assigned to it by the programmer.

�There are four types of storage class available in C: Auto Extern Static Register

Auto �All local variables has this storage class. �Default value is the garbage value. �Scope of the variable is only between the blocks where it is declared. �Lifetime is till the control remains within the block or function where these variables are defined. �These variables are destroyed whenever block ends or function jump occur. �To declare auto storage class auto keyword is used.

Auto Cont. Storage − Memory. Default initial value − An unpredictable value, which is often called a garbage value. Scope − Local to the block in which the variable is defined. Life − Till the control remains within the block in which the variable is defined.

�Example: auto int n; �Auto keyword is optional all the local variables by default fall under this storage class. �Example: int n;

Example main( ) { auto int i, j ; printf ( "n%d %d", i, j ) ; } Output: - 1211 221


Output of Program 10 20 10 30 10

Example main( ) { auto int i = 1 ; { { { printf ( "n%d ", i ) ; } printf ( "%d", i ) ; } }

Output 1 1 1

Extern �Scope is through out the program. �Lifetime is till the end of the program. �Initial value is 0. �Extern keyword is used to declare the variable of this storage class. extern int x; �By default global variable has this storage class.

Extern Cont. Storage − Memory. Default initial value − Zero. Scope − Global. Life − As long as the program’s execution doesn’t come to an end.

Extern Example #include <stdio. h> void increment( ); void decrement( ); extern int i=1; void main( ) { printf("%dn", i); increment( ); printf("%dn", i); decrement( ); printf("%dn", i); } void increment( ) { i++; printf("%dn", i); } void decrement( ) { i--; printf("%dn", i); }

Output 1 2 2 2 1 1 1

Static �It is special case of local variable. �These are defined inside the function or block. �Its scope is inside the block or the function where it is defined. �Initial value is 0. �Its value is retained between different function calls. �Lifetime is same as the global variable i. e. through out the program. �Keyword static is used to define this type of variable.

Static Cont. Storage − Memory. Default initial value − Zero. Scope − Local to the block in which the variable is defined. Life − Value of the variable persists between different function calls.

Static Example #include <stdio. h> void increment(); void main() { increment(); } void increment() { static int i=1; printf("%dn", i); i++; }

Output 1 2 3 4

Auto Example #include <stdio. h> void increment(); void main() { increment(); } void increment() { int i=1; printf("%dn", i); i++; }

Output 1 1

Register �Register variable behave in every way same as the auto variable. �The only difference is that register variable are store inside the computer register instead of the memory. �They are used when CPU has to access the variable very frequently. Eg looping variable �They are defined by placing keyword register before the datatype of variable. Example register int a=10;

Register Cont. Storage - CPU registers. Default initial value - Garbage value. Scope - Local to the block in which the variable is defined. Life - Till the control remains within the block in which the variable is defined.

main( ) { register int i ; for ( i = 1 ; i <= 10 ; i++ ) printf ( "n%d", i ) ; }

Example int x = 10 ; main( ) { int x = 20 ; printf ( "n%d", x ) ; display( ) ; } display( ) { printf ( "n%d", x ) ; } OUTPUT: - ?

Output 20 Local Value gets the preference

Which to Use When � Few ground rules for usage of different storage classes in different programming situations (a) economise the memory space consumed by the variables (b) improve the speed of execution of the program

− Use static storage class only if you want the value of a variable to persist between different function calls. − Use register storage class for only those variables that are being used very often in a program. Reason is, there are very few CPU registers at our disposal and many of them might be busy doing something else. Make careful utilization of the scarce resources. A typical application of register storage class is loop counters, which get used a number of times in a program.

− Use extern storage class for only those variables that are being used by almost all the functions in the program. This would avoid unnecessary passing of these variables as arguments when making a function call. Declaring all the variables as extern would amount to a lot of wastage of memory space because these variables would remain active throughout the Life of the program. − If you don’t have any of the express needs mentioned above, then use the auto storage class. In fact most of the times we end up using the auto variables, because often it so happens that once we have used the variables in a function we don’t mind loosing them.