Storage classes in c language

TanmayModi3 395 views 15 slides Jul 25, 2019
Slide 1
Slide 1 of 15
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15

About This Presentation


Storage Classes in C

Storage Classes are used to describe the features of a variable/function. These features basically include the scope, visibility and life-time which help us to trace the existence of a particular variable during the runtime of a program.


Slide Content

Storage Classes in C

Introduction Variables declared in C programs are different than other programming languages. Can use the same variable names in the C program in separate blocks. When we declare a variable,it is available only to a specific part or block of the program. Remaining block or other functions cannot access the variable. Variables declared within functions are called local variables and those declared outside are called external or global variables.

Definition Scope of variables: The area or scope of the C program from where the variable can be accessed. The area or scope of the variable depends on its storage class i.e. Where and how it is declared. The storage class of a variable tells the compiler: i )the storage area of the variable i i)the initial value of the variable. Iii)the scope of the variable. Iv) the life of the variable i.e. How long the variable would be active in the program.

Variables declared in C can have any one of the four storage classes: Automatic External Static Register

Lifetime of a Variable Every variable has a lifetime i.e. Its time duration during which its status is active in the program. The time gap between its declaration and ckeanup . The lifetime depends upon the storage class. For eg,auto variable gets destroyed immediately when the function execution is over,whereas static variable remains in the memory.

Visibility of a Variable It defines the scope of a variable. The scope is of two types: Local scope Global scope Global is recognized throughout the program,whereas the local variable scope is limited to the declaration block

Automatic variables The auto keyword is applied to all local variables automatically. It is the default storage class that is why it is known as automatic variable. the scope of the variable is local to the block in which it is defined,available only to the current block or program. The vanish after the block of code finishes executing.

Example:

Register Variables The register variable allocates memory in register than RAM. It has a faster access than other variables. CPU registers are limited,hence cannot declare more register variables. It is recommended to use register variable only for quick access such as in counter . Note: We can't get the address of register variable.

Example void main() { register int m=1; clrscr (); for (;m<=5;m++) printf (“\t % d”,m ); } 1 2 3 4 5

Static variables The  static  variable is initialized only once and exists till the end of the program. It retains its value between multiple functions call. The static variable has the default value 0 which is provided by compiler,initialised once,it is never reinitialized. Can be used to count how many times the function was called.

Example

External Variable The extern variable is visible to all the programs. It is used if two or more files are sharing same variable or function . In case in aprogram where both external and auto variables have been declared with the same name,the first priority is given to the auto variable. Variable defined at other place,other than the place where it is declared.

Example: i nt j=4; v oid main() Output: { extern int j; clrscr (); j=j*3; printf (“ j=% d”,j ); fun(); printf (“\n j=% d”,j ); } f un() { j=j*j; } J=12 J=144