1 / 12

Storage Classes

Storage Classes. Storage Classes. Storage class tells : 1) Where the variable is stored. 2) Initial value of the variable. 3) Scope of the variable. (which a variable is accessed.) 4) Life of the variable. Storage Classes. There are four types of storage classes:

ninon
Download Presentation

Storage Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Storage Classes

  2. Storage Classes • Storage class tells : • 1) Where the variable is stored. • 2) Initial value of the variable. • 3) Scope of the variable. (which a variable is accessed.) • 4) Life of the variable.

  3. Storage Classes • There are four types of storage classes: • 1) Automatic storage class • 2) Register storage class • 3) Static storage class • 4) External storage class

  4. Storage Classes • Automatic storage class • Variable is stored in memory. • Default value is garbage value • Scope is local to the block where it is declared • Life is, with in the block in where it is declared • Automatic variable can be declared using the keyword auto • Eg: auto int a; • By default all variables are automatic • int a; same as auto int a;

  5. Storage Classes • Example 1: • main() • { • auto inti=10; • printf(“%d”,i); • } • Output: • 10 • Example 2: • main() • { • auto inti; • printf(“%d”,i); • } • Output: • 1002 • In example 1, i value is initialised to 10.So,output is 10. • In example 2, i value is not initialized. So,compiler reads i value is a garbage value.

  6. Storage Classes • Register storage class • Variable is stored in a register instead of RAM. • Default value is garbage value • Scope is local to the block where it is declared • Life is, with in the block in where it is declared

  7. Storage Classes • Register storage class • When a variable stored in register, it access high speed • The no of registers are limited in a processor, if the registers are not free it will convert to automatic variable • The mostly length of the registers are 2 bytes, so we cannot store a value greater than its size, if it so it will covert to automatic variable • Register variable can be declared using the keyword register • Eg: register int a;

  8. Storage Classes Static - Storage Class • Variable is stored in memory. • Default value is 0 • Scope local to the block. • Life is, value of the variable persists between different function calls. • static variable can be declared using the keyword static • Eg: static int a;

  9. Storage Classes Static - Storage Class • static can also be defined within a function. • The variable cannot reinitialized when the function is called. • This inside a function static variable retains its value during various calls. • main() • { void show(); show(); show(); • } • void show() { • static int a=10; • printf(“%d”,a); • a++; • } • o/p will be 10 11 • second time a=10; will not execute

  10. Storage Classes • External - Storage Class • Variable is stored in memory. • Default value is 0 • Scope is end of the program • Life is, with in the block in end of the program • external variable can be declared using the keyword extern • extern is used to give a reference of a global variable that is visible to ALL the program files.

  11. Storage Classes • External - Storage Class • extern int a=10; • void main() • { • vid show(); printf(“a= %d”,a); show(); • } • void show() • { • printf(“%d”,a); • } • Output a=10 10

  12. Storage Classes • External - Storage Class • The extern keyword is used before a variable to inform the compiler that the variable is declared some where else.

More Related