Вы находитесь на странице: 1из 6

Chapter 1:

Storage class:
Points to remember:

Storage classes

1) Storage class is the specifier that determines the behaviour of a variable. In other words, the storage class of a variable is the thing that determines which parts of the program can access it and how long it stays in existence. 2) There are 4 types of storage classes. They are : a) Automatic Storage Class. It is the storage class that determines the lifetime automatic variables. b) External Storage Class. It is the storage class that determines the lifetime external or global variables. c) Static Storage Class. It is the storage class that determines the lifetime and variables. d) Register Storage Class It is the storage class that determines the lifetime register variables.

and scope of and scope of scope of static and scope of

3) The behaviour of a variable refers to the scope and lifetime of that variable. 4) The scope of a variable refers to the part of the program in which it is active or it is visible. In other words, the scope of a variable refers to the visibility of that variable to the one or more blocks or functions in the same program or other programs. 5) The lifetime of a variable refers to the time duration or period between its creation and destruction. In other words, lifetime of a variable refers to the time duration or time period up to which the variable retains its value. 6) The idea behind limiting the lifetime of variables is to save the memory space. If a function is not executing, the variables it uses during execution are presumably not needed. Removing them frees up memory that can then be used by other functions. 7) Based on data type, the variables can be classified as char, int, float and double.

8) Based on storage classes, the variables can be classified as a) Automatic variables b) External variables c) Static variables d) Register variables 9) Automatic variables: 1. They are declared by using the keyword auto in a C/C++ program. 2. They are always declared inside a function in which they are used; that is their existence is known to only that function in which they are declared. Hence, they have no scope outside their function ( in other words, their scope is that function in which they are declared and defined ). 3. They are automatically created ( they are assigned value now ) only when the function, in which they are declared, is invoked ( called by a certain function ) and are automatically destroyed ( their values are lost )when the execution of the function in which they are declared ceases or is over. Hence, they are known as automatic variables. 4. Any variable without its storage class specifier, by default, is considered as automatic variable. Hence, while declaring automatic variables, it is not necessary to use the keyword auto . 5. The lifetime of an automatic variable coincides with the time when the function in which it is defined is executing. That is, the lifetime of automatic variables is the time duration of execution of the function in which they are declared. 6. When the automatic variables are not initialized, whenever they are declared within a function, then their default value is garbage value ( i.e., they can take any value ). 7. Program Examples:

// 1. program with the keyword auto. #include<stidio.h> #include<conio.h> void main( ) { auto int i; clrscr( ); i = 10; printf( \n\n\t The value of i is : %d , i ); getch( ); } // end of the main( ) function

// 2. program without the keyword auto #include<stdio.h> #include<conio.h> void main( ) { int i = 20; clrscr( ); printf( \n\n\t The value of i is: %d, i ); getch( ); } // end of the main( ) function 10) External Variables

Definition : These are the variables that are declared outside of the definitions of all functions ( including main( ) function ). Points to remember 1. They are declared outside of the definitions of all functions of a given program. Hence, they are global in nature. That is, their existence is known to all functions in the entire program. Hence, they are known as global variables. 2. They are stored in main memory ( RAM ) but not in CPU registers. 3. Their scope is global ( i.e., the entire program ) 4. Their lifetime is equal to the time taken for execution by the program in which they are declared. 5. If they are declared before any function including main( ) function then their values can easily be accessed by all functions without using the keyword extern. And if they are declared after some functions then they are declared in such functions with the keyword extern. 6. examples 1. Without using the keyword extern #include<stdio.h> #include<conio.h> // global declaration int num ; // function declaration or function prototypes void Increment( );

void Decrement( ); void main( ) { num = 10 ; clrscr( ); printf( The value of num is %d , num ); // calling user defined functions to access the value num Increment( ); Decrement( ); getch( ); } void Increment( ) { num= num + 5 ; printf( \n\n\t the value of num is %d , num ) ; } void Decrement( ) { num= num - 5 ; printf( \n\n\t the value of num is %d , num ) ; }

2. Using the keyword extern

11) Static variables : Definition : These are the variables which retain their assigned values the same throughout the program. Points to remember : 1. They are declared using the keyword static 2. There are two types of static variables : 1) Internal static variables and 2) External static variables 3. Internal static variables :

Internal static variables are local variables. Hence, their existence is known only to that function in which they are declared. Therefore, they are identical to automatic variables, except that the static variables retain the same value through the program. // program example # include<stdio.h> void sum( ); void main( ) int index; for( index = 0; index <= 3 ; index ++ ) sum( ); } // end of main( ) function void sum( ) { static int sum = 0; sum = sum + 10 ; printf(\n\n\t sum is %d, sum ); } // end of sum( ) function OUTPUT : Sum Sum Sum Sum is is is is 10 20 30 40 {

4. External static variables : External static variables are global in nature. Hence, they are declared outside of all functions including main( ) function. They are visible to all functions in the program. 5. static variables are initialized only once during the compile-time. They are never initialized again. 6. the initial values to be assigned must be always constants but not expressions. 7. by default they are initialized to zero.

12) Register variables: Definition : These are the variables whose values are stored in the CPU register rather than the RAM. Points to remember : 1. They are declared using the keyword, register. 2. They are local to the function in which they are declared. 3. CPU register are fast in information exchanging ( reading or writing ). Hence, by using the register variables, the execution speed of the program can be increased. 4. If the CPU register are not free for holding or storing the data, then the register variables behave as automatic variables.

Вам также может понравиться