C language
Storage Class
storage classes tell you about for things
1)auto -meomory create at run time
initial value-garbage value
memory location-stack
scope of variable-Local
lifetime of variable-Till control remain with that function
initial value-garbage value
memory location-stack
scope of variable-Local
lifetime of variable-Till control remain with that function
Storage Class
storage classes tell you about for things
- initial value
- meomory location
- scope of variable
- lifetime of variable
1)auto -meomory create at run time
-declare inside main( )
-in variable default is auto
- //even if u don't write auto then also program works same
- #include<stdio.h>
- #include<conio.h>
- int main()
- {
- double goa;
- auto int i; //here i don't have any value therefore it will print some garbage value
- printf("i=%d",i);
- goa=addi();
- printf("goa i=%d",goa);
- getch();
- return 0;
- }
- int addi() /
- {
- auto int i ;
- printf("add i=%d",i); //now here i value will change as this is auto and scope of that i ended
- return i;
- }
- /*u know how scope overs ? i tell u this code works in stack area of ram and when scope overs
- then in stack meomory previous i value get poped i.e it get deleted.
- and Now as soon as i get initialised in addi() at that time lifetime of previous i variable get
- over */
summary
- initial value-garbage value
- meomory location-stack (in every case meomory created in stack at compile time)
- scope of variable-Local
- lifetime of variable-Till control remain with that function
2)register -meomory create at compile time
-store in register and not in ram
-for very fast access suppose you have make counter
- #include <stdio.h>
- #include <conio.h>
- //register storage class ..very fast storage class use in cpu
- int main()
- {
- reg int i;
- if(i--)
- {
- printf("%d",i);
- main();
- }
- getch();
- return 0;
- }
summary
-same summary as auto the only difference here accessing is through cpu rather than ram due to which it is a quick access
3)static-meomory create at runtime
- #include <stdio.h>
- #include <conio.h>
- //no i outside
- int main()
- {
- static int i=3;
- if(i--)
- {
- printf("%d",i);
- main();
- }
- getch();
- return 0;
- }
in above program your output will be 210 but now in above program if u don't write keyword static i.e if u write code
- #include <stdio.h>
- #include <conio.h>
- int main()
- {
- int i=3;
- if(i--)
- {
- printf("%d",i);
- main();
- }
- getch();
- return 0;
- }
then you will get 22222222........infinite times as in that case there will be just int which is auto storage class as we know . so, in this case meomory will be created at runtime .
IN COMPILER PROGRAM FIRST COMPILED THEN COMES ON RUNTIME
SO U CAN SAY STATIC NOT SUPPORT RECURSION
summary
- initial value-0 and not garbage value
- memory location-stack
- scope of variable-Local
- lifetime of variable-Till control remain with that function
4)extern-meomory create at runtime
With this storage class you can access same common variable in any function all across program
- #include <stdio.h>
- #include <conio.h>
- int i=3; //i declare outside here
- int main()
- {
- extern int i; //this statement means don't create seprate meom for i here as i is already
- //created globally
- //let if u write here i=5 then it will change i value all across program.
- if(i--)
- {
- printf("%d",i);
- main();
- }
- getch();
- return 0;
- }
HERE IN CASE IF U DON'T INITIALIZE YOUR VARIABLE GLOBALLY THEN U WILL GET AN ERROR.
summary
No comments:
Post a Comment