53 - STORAGE CLASSES - C PROGRAMMING

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends our Channel so indecision let us have a look on storage caches New Zealand so before going to the actual concept of this storage crisis we need to know what is the use of storage gases what is meant by storage classes so for every variable which way are they declaring in our C program is having some specific scope and lifetime so for every variable which we are declaring in our C program we'll have the scope and lifetime so what is meant by a scope and what is it like that so school is in a region then the variable can be used region death can be used so we have variable can be used and the life thing means how long the variable force death memory so it means how long a variable force so this is the scope and lifetime for every variable there will be scope and lifetime so depends upon the Declaration point thus corporate life that will be vary so that we call it as a storage classes and mainly there are four storage classes in our C program so first one auto variables second one static variables third one external variables fourth one registers so these are the four storage classes available in our C language and compulsory the variable if we declare a variable it will be declared by using any one among these storage castles so for this what is the syntax for declaring it students gasp so syntax for declaring is using the storage cache so storage gas storage gas followed by theater pipe for a point where he will name storage gas for our identity followed by variable name simple so actually we have seen the declaration of a variable if you want to declare an integer variable we're simply anything else it I so this indicates we are declaring a variable I as an integer datatype similarly the storage less here in this storage cache we have to use a keyword of one among these four so the key word for this automatic variables are also this static this is X term this one register so these are the key words which we have to use while declaring any variable using the storage class so if you want to declare a variable using the automatic variable we have to write it as R to eat I so this statement implies we are declaring the variable I as an integer represent and having the storage class of the next static in ie so it implies we're declaring a variable I as an integer data type which follows the storage class standing similarly if you use extent we have to use X term in I next X sorry resistant in I so what is the difference between all these false college classes the main difference between all these pushed for storage classes is at school at school and life type of variable so the scope and lifetime of particular variable will be changing for each and every storage gas so now let us see the scope and lifetime for our two static and register so if you if you simply declared it as int ie the default storage Cass is our tool so if you declare an integer variable I as an integer datatype if you if you are not mentioning the storage task automatically the compiler will treat this vehicle declaration as automatic variable so it will treat is an automatic variable so whatever the spoof interactive having with auto variables will be applied to the variable right so by default storage gas is our to automatic where so as it is a cases to the keyboard sound all and it's more letters you should not write in capital letters Auto in here also static extent and register so now let us see the scope and rectum of each and every stone it's gas so let us begin with automatic minutes this is simply like local variables which were declining in the program automatic variables which we are declaring in a program there's a lot of variables which are declaring in the regular program that means the school means Canadian well it can be used means then the variable so cash can be used in the same block in which the variable has been declared so only in the same block in which plunk we're declaring the variable in the same block itself the variable can be used that means if we declare the variable in a main function we can use the variable only in the main function so we can't use the variable in the predefined offering a user-defined function so actually we are writing the user-defined function outside the main function so we cannot use the variables which are declared in main function in the user-defined function so what we call it as Auto and simply we can sell these variables as a local variables so local variables also can be used only within the drop which in which the variable can be declared and what about the lifetime here also so after when the variable forced a memory so only only the Block in which the variable has been declared that means alive the variable will be alive only within the same block in which the variable has been declared immediately after executing this block after ending the this block in which the variable X particular automatically release everybody that means the variable will be dead so the variable will be only inside the block in which the variable has been detained so if the variable is declared in the main function immediately after the paint function the variable will be lazy then memory which folds which it or stir throughout the program so the limitation also we can you know so that's why you will say that automatic variables are the local variables then what is a default value so as we know that the default value of any variable declare any local variables is it that man so the Prem's the default value means after declaration after declaring a variable and before initializing a variable the value in value in the memory is a garbageman that means if you don't initialize any data to the variable that means if you don't initialize the value to the variable the default value will be there that means well it means 8i this implies so on photo in eye this implies and memory will be allocated to bytes of memory will be and my differently so only the declination is here there is no additional ization until the hiding is initialized the value of this memory will be back value that is a default value is it that Beach value if the variable is declared using the outpost to ditch class now moving on to the second one second one is a static so what is a static here also the syntax is static int Arya but in place is a variable declared using in and follows the scope and lifetime of static storage Castle scope so after where these variables will be used where the in which region the variable can be used so this variable can be used in the talk in the same block in which the variable has been declared the pins the scope is same for auto and static variables so in which block we are declaring the variable after that properly we can use them that particular variable so outside the brophy if we can't use this particular variable next lifetime so what is the lifetime often static variable so here the life thing means away up to break then the very police alike so here until the completion of the program completion of the program the variable will be away through the program the variable will be alive so after completing the company the program execution there only the variable will release the memory so our program may consists of main function and user-defined function so the variable cannot be used in Protheroe the main function and the rock a user-defined function but the variable will be steamed away until the program completes its execution so this can be clearly understood was writing the simple example that we will write later then what is the default value default value off a static variable is it zu not I got this value is C 0 so the default value of a god-awful static variable is zeros and things if you declare a variable static int ie memory is allocated with the two bytes and the value in the memory location is 0 because the manor is step one of the third one turns to this gas third storage gas is external so external storage cache is similar to the global V difference so I'm not actually is a global variable declaration we can the process is similar to the global variables so the syntax for declaring any variable using X term is the external int I that means we're declaring a variable I as an integer data type and it follows the scope and wave type of external storage gas so here actually this external means it says the compiler somewhere else the declaration has been done so either it may be outside the main function inside the main function and inside the user-defined function whatever it may be plays it gives the compiler to check for the initialization of a particular variable so he'll the scope the scope of external variable is true of the program so we can access the variables which are declared as external throughout the entire program throughout the program next what is the lifetime so here also the lifetime is similar to the static variables so it will be away until the program completes its execution program completes its execution it is right so after completing the program execution their own little memory it will be released by the external Network and what are the default values what is the default value here also it is similar to the static that is zeros so a memory will be different for this statement it is named as I whose results are two bytes of memory and the value will be 0 because here there is no initialization to political nature so the default value will be zeros next there is water the 4 month registers so here actually the values if any available is declared using a resistor so the compiler will allocate one resistors of a processor to them to store the value of a variable so here instead of storing the value in the RAM it will store the value in these resistors so it stores the value e resistors so first it will check for the resistor which is available if the register is available automatically the value will be stored in the registers so if the resistor is not available then automatically it was stores in the memory so here also so what is the purpose of storing the value the distance so it gives fast axis during the one time so it gives the fast access to that particular variable which is stored in the register during the runtime of a program now what is the scope so here also the scope is similar to the automatic variables that means within the block within the block in which variable has been declared and whatever the wise thing so to ban the variable or the variable will be alike this is also similar to our automatic mean so only within the block in which the variable has been declared immediately after completion of the particular block where the variable is declared automatically the variable will release the memory release is remembered so the rough time is also within the flow that means I am only within the where the variable is declared what about the default man so here the default value is a garbageman management so this is completely similar to our automatic variable the but only difference is thus where the data is stored where the value will be stored so in the automatic variables the value will be stored in RAM there is a memory and here the first preferences on resistors so first the compiler will check for availability of resistors if any resistor is available automatically the variable the value of that particular variable is stored into the resistor and if the resist is not available because there will be a limited registers in hmmm processor right so if the register is not available then automatically the storage area is a member so the values will be stored in the memory so that main purpose of storing the main advantage of storing the values into the registers is we can access the variables fast so we can access the resistors fast the data inside the registers will be excessive fast during the runtime of the program for this purpose we will use the registers so this is all about the storage classes now let us see an example for the static and external so automatic variables are normal variables of so in the past videos whatever the program's we are using whatever the variable declarations we are using all outcomes of them this local variables that means an automatic variables so then we are not hiding any storage cache because we're just defining the variables as nd a comma B comma C that means automatically these variables will follow the life time and scope of it automatic variables so then that is by default it is declared as art of storage cache so the rule we have we have to follow is we have to declare all the variables in the program all the variables before using those variables inside the program so while declaring the variable it is mandatory to specify the storage cache for that particular variable so for our purposes we are not declaring any storage classes so by default the compiler will treat that storage cache as an author so now let us see an example for static and the external storage classes so where we can the library school now let us see the difference between this automatic variables and the static variables so come to this automatic variables and writing the program and starting from the main function so main here we just declare the we defined at a user-defined functions we call it as increment so we can call the same function three times again increment again implement so automatically we have to write the code for this use a different function outside the main function so I am writing the code increment so I am declaring the variable using auto that means also int J is equal to 0 j is equal to k plus 1 and just incrementing the value of EI and I am just printing the value of hearings equal to percentage deep close now I will write the same code and instead of creating declaring the variable J using Auto I will create or I will declare the variable using step the same function so main again define three functions so because three functions the same function we have bigger than three types close sighs it is a user-defined function we have to write the definition of user-defined function here outside the papers so I am writing the function same function implement declare variable using a static static in a is equal to zero and just increment that variable a is equal to a plus one and printer a is equal to percent a CD a rose same program I have written here both are same but the only difference is here I am declaring the variable a using Auto storage class and here I am declaring the variable a using static storage here's the difference of both storage cache is lifetime scope is same so here the scope means where where the variable should be used so here also we have to use the a value only inside them use a different function so a value can be used in the main function here also a can be used only in sensor user-defined function because in the user different function only we have declared the variable a and we return we should not use this variable in the main function we can't access the variable in the main function now so after executing this one first the compiler will the control will execute this increment automatically the control wave interviewed this definition so it a is equal to 0 Y is equal to a plus 1 printf a so yeah will be printed as 1 and the lifetime of this auto is within the same block so where the variable is declared the variable will be live up to that Patrick Lach so after country after printing the value of yay the block ends so automatically the memory followed by the where every will be released like this available will be dead it is not aligned it is dead variable now again after reaching the end of the statement automatically the controller will move on to the next statement in the main function again we're following the increment function again the control will start executing the function definition so again not too wrinkly is equal to zero again a variable a signal nowadays could react this one so here is put 0 it is equal to at this one so again J is equal to 1 will be printed you will be free after executing this printf statement remember it reaches the end of the clock automatically the memory followed by the variable will be released that means the variable will be dead yeh will be dead so here J is LM only up to this block right so whenever the control reaches the end of the function definition automatically the variable will be dead so all the memory which is occupied by the variable will be waste so hey but control we will move on to the next statement of main function again we are declaring again we have declared the again we have called the increment function so again the controller will move on to the function definition again a variable is different using the auto and a is incremented is printed so again it is equal to 1 so output of this program is y is equal to 1 is equal to 1 is equal to 1 because we have declared the increment function three times so this is the execution of program where we have a Pickler the variable using auto students coming to this here we have declared the variable as a static so the scope is same and the changes it lifetime lifetime means until the completion of the program the variable will be alive not only within the block until the program is executed completely the variable will be in it that means all the memory before it by that variable will not be released right so no again we have the controller reaches the function call immediately the control will move on to the function definition and then the Santaquin da is equal to zero a is equal to one increment it and three J is equal to one will be printed and whenever it reaches the end of the function and the control will move on to the next statement in the wave function and the variable will not be dead because here we are declaring the variable because the program has not been complicated again still there are two more statements to the main function so until you complete these two statements in the main function the variable will be and that means the memory will not be relational which is folded by the particular variable because here we are declaring the variable using static storage class so again increment will be gone again of control we come to the function definition and this statement will not execute it because the available is still alive so this statement will not be executed so what really is equal to J plus 1 so J is equal to 2 so previously is for the 1 so is equal to 2 and then create a V is equal to be able to do it how many it reaches here it will say for the main function now that the main function all the statements in the main function has been completed or not if there are any more statements available will be still in it LF that means again we are calling the increment and a patron will move on to the function definition so again this statement will not be declared I mean will not be executed again a will be implemented so a is equal to 3 freedom so whenever it reaches an another block if you change for the statements in the main function so here all the statements in the main function has been complicated so the complete program has been completed its execution now the variable a will be dead that means all the memory which is folded by the variable here will be released so the memory will be folded until the complete main function that means the complete program has been executed so that is the the output for this program memory declare a variable using the static storage cache is equal to one two and three and the scope is similar to them utter many variables we cannot use this a value inside the main function because a is declared in the user different function so here should be used only in the user different function they cannot be used in the main function so hope you understood the difference between automatic variables and the static variables now let us move on to the X 10 X 10 foolishness so that is also very simple so as I said the the scope of X term is through the program and the lifetime is also throughout the program and here what is the use of extra so we need not initialize the variable in the main function during the declaration or in the main function we can initialize the variable which is declared using the extra storage cache anywhere in the program so first is the compiler wherever it reaches the statement extent that means declaration it shows that that particular variable has been initialized somewhere else in the program so first it will check for the initialization of the extent variable and then we can use the variable value inside the main function so first let us write the program so that you can clearly understood so main function so I have declared the x value x is equal to 3 and also declaring the value the extern y is equal to five sorry extend why extend int Y so that means I have declared the variable by using in detail datatype and it should follow the life and scope of rectangle scope of external storage class now I am just printing the values printf yes is equal to percentage D yes similarly printf Y is equal to percentage D by frozen-in version so what is the output for this Here I am printing the X so X will be 3 will be written and why Y is equal to 4 so here there is only declaration but no initialization so whenever the control reaches the statement X term wherever it reaches this X term storage class the compiler will say that somewhere else in the program the way Y is initialized so Y has been initialized somewhere else in the program so first it will check for the initialization of Y so if it doesn't find the visualization of Y if we print the different so the people value of external data type is Z n so the output of this program is 3 and 0 so and it will be changed after the main function I am initializing the Y as 10 after the main function right so here immediately for the first premium X will be printed as 3 and the second printf right so the second statement X turn 8 money so it it shows or it says that the ye initialization the value of I has been lies somewhere else in the program so the program includes not only the main function also the user-defined function so it will check the complete lines of code there the way has been initialized so here the Y has been initialized so M will be assigned to Y in this statement itself so if you read them if you print the value inside the main function even though the Y has been declared I mean the guy has been initialized after the main function the value will be printed here Ken so that is the usage of extent so we did not initialize the value inside the main function we can initialize the value outside the main function also so first the compiler will check for initialization of Y through the program and it will assign the value to the particular variable so in the main function we are writing them we are printing them by value so first it will check for the Y value throw to the program so at the end of the main function I would say the main function we can find the value sub y value initialization so 10 will be initialized to Y and Y will be pinned it here so this is the advantage of external storage games so hope you understood this storage class's concept it's very very important and the most important thing is we have to declare a variable using any one among these four storage guests we have to mention the storage caste while declaring the variable so if you not if you are not mentioning the storage class so that piss you off or if you are not declaring any variable using this any one of these storage gasses by default the compiler will treat that declaration has an automatic variables so the lifetime and scope of automatic variables will be applied to the particular variables which are declared without using any storage gasses hope you understood this one and thanks for watching my sessions and if you are having any doubts regarding the C programming feel free to post your doubts in the comment section and if you like my videos share my videos with your friends and subscribe to my channel for getting more technical videos in various subjects thank you for watching thank you very much
Info
Channel: Sundeep Saradhi Kanthety
Views: 82,530
Rating: 4.9021955 out of 5
Keywords: sundeep, saradhi, kanthety, c language, c programming, cp, computer, computer programming, loops, Functions, calling function, modules, modular programming, return type, function name, arguments, return, addition using functions computer training, c language trainee, c language training, cp training, cp sessions, scope, lifetime, variable, storage class, auto, static, extern, register, keyword, local variable, global variable, garbage values, automatic variables, default values
Id: 4Qm8FeBQMfY
Channel Id: undefined
Length: 38min 7sec (2287 seconds)
Published: Fri Dec 15 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.