+ All Categories
Home > Documents > S torage class in C

S torage class in C

Date post: 07-Aug-2015
Category:
Upload: kash95
View: 16 times
Download: 2 times
Share this document with a friend
Popular Tags:
11
SHUBHAM BRAT VIKASH KUMAR PATWA
Transcript
Page 1: S torage class in C

SHUBHAM BRAT VIKASH KUMAR PATWA

Page 2: S torage class in C

INTRODUCTIONINTRODUCTION

• Not only variables have data types but also a storage class..Not only variables have data types but also a storage class..

• Where the variable would be stored.Where the variable would be stored.

• Default initial value if not specifically assigned.Default initial value if not specifically assigned.

• Scope i.e in which function the value would be available.Scope i.e in which function the value would be available.

• How log would the variable exist.How log would the variable exist.

Page 3: S torage class in C

CLASSIFICATIONCLASSIFICATION

• Automatic storage classAutomatic storage class

• Register storage classRegister storage class

• Static storage classStatic storage class

• External storage classExternal storage class

Page 4: S torage class in C

AUTOMATIC STORAGE CLASSAUTOMATIC STORAGE CLASS

Are declare inside a function in which they are to be Are declare inside a function in which they are to be utilized.utilized.

Are declared using a keyword Are declared using a keyword auto.auto.

eg. auto int number;eg. auto int number;

Are created when the function is called and destroyed Are created when the function is called and destroyed automatically when the function is exited.automatically when the function is exited.

This variable are therefore private(local) to the function This variable are therefore private(local) to the function in which they are declared.in which they are declared.

Variables declared inside a function without storage Variables declared inside a function without storage class specification is, by default, an automatic variable.class specification is, by default, an automatic variable.

Page 5: S torage class in C

EXAMPLE PROGRAMEXAMPLE PROGRAMint main()int main()

{ int m=1000;{ int m=1000;

function2();function2();

printf(“%d \n”,m);printf(“%d \n”,m);

}}

function1()function1()

{{

int m = 10;int m = 10;

printf(“%d\n”,m);printf(“%d\n”,m);

}}

function2()function2()

{ int m = 100;{ int m = 100;

function1();function1();

printf(“%d\n”,m);printf(“%d\n”,m);

}}

Output101001000

Page 6: S torage class in C

EXTERNAL STORAGE CLASSEXTERNAL STORAGE CLASS

• These variables are declared outside any function. These variables are declared outside any function.

• These variables are active and alive throughout the entire program.These variables are active and alive throughout the entire program.

• Also known as global variables and default value is zero.Also known as global variables and default value is zero.

• Unlike local variables they are accessed by any function in the program.Unlike local variables they are accessed by any function in the program.

• In case local variable and global variable have the same name, the local variable will have In case local variable and global variable have the same name, the local variable will have precedence over the global one.precedence over the global one.

• Sometimes the keyword Sometimes the keyword externextern used to declare these variable.used to declare these variable.

• It is visible only from the point of declaration to the end of the program.It is visible only from the point of declaration to the end of the program.

Page 7: S torage class in C

EXTERNAL VARIABLE (EXAMPLES)EXTERNAL VARIABLE (EXAMPLES)int n;int n;main()main(){ increment();{ increment();increment();increment(); decrement();decrement();}}increment()increment(){n=n+1;{n=n+1;Printf(“ inc value %d”,n);Printf(“ inc value %d”,n);}}decrement()decrement(){n=n-1{n=n-1 Printf(“ inc value %d”,n);Printf(“ inc value %d”,n);

}}

int x=10;main(){int x=20; printf(“\n %d”,x); display();}display(){printf(“\n %d”,x);}

The variable n is available for use in all three function

When the function references the variable count, it will be referencing only its local variable, not the global one.

Page 8: S torage class in C

STATIC STORAGE CLASSSTATIC STORAGE CLASS• The value of static variables persists until the end of the program.The value of static variables persists until the end of the program.

• It is declared using the keyword It is declared using the keyword staticstatic like like

static int x;static int x;

static float y;static float y;

• It may be of external or internal type depending on the place of there It may be of external or internal type depending on the place of there declaration.declaration.

• Static variables are initialized only once, when the program is compiled.Static variables are initialized only once, when the program is compiled.

Page 9: S torage class in C

EXAMPLE OF STATICEXAMPLE OF STATIC

• Internal static variable can be used to count the number of calls made to function. eg.Internal static variable can be used to count the number of calls made to function. eg.

int main()int main(){{ int I;int I; for(i =1; i<=3; i++)for(i =1; i<=3; i++) stat();stat(); }}void stat()void stat(){{ static int x=0;static int x=0; x = x+1;x = x+1; printf(“x = %d\n”,x);printf(“x = %d\n”,x);}}

Output

x=1

x=2

x=3

Page 10: S torage class in C

REGISTER STORAGE CLASSREGISTER STORAGE CLASSThese variables are stored in one of the machine’s register and are declared These variables are stored in one of the machine’s register and are declared

using register keyword. using register keyword.

eg. register int count;eg. register int count;

Since register access are much faster than a memory access keeping Since register access are much faster than a memory access keeping frequently accessed variables in the register lead to faster execution of frequently accessed variables in the register lead to faster execution of

program.program.

Since only few variable can be placed in the register, it is important to Since only few variable can be placed in the register, it is important to carefully select the variables for this purpose. However, C will automatically carefully select the variables for this purpose. However, C will automatically

convert register variables into nonregister variables once the limit is convert register variables into nonregister variables once the limit is reached.reached.

Don’t try to declare a global variable as register. Because the register will be Don’t try to declare a global variable as register. Because the register will be occupied during the lifetime of the program.occupied during the lifetime of the program.

Page 11: S torage class in C

Recommended