Introduction To Storage Class, The Auto Storage Class In C

Shibaji Paul
A free video tutorial from Shibaji Paul
Programming Instructor with 16+ years of experience
4.4 instructor rating • 7 courses • 22,537 students

Learn more from the full course

C Programming Step by Step - Complete Tutorial For Beginners

C language tutorial from basics with C operator,loop,array,pointer,function,parameter,string,recursion,structure,file.

17:18:44 of on-demand video • Updated June 2020

  • if else statements, loop - while, for and do while loop with many examples.
  • Array - 1D and 2D, why we need them and how to use them effectively.
  • String in C - NULL terminated character arrays.
  • Writing function, parameter passing to function. Returning value from function.
  • storage class - auto, static, extern and register
  • Pointer - in depth understanding.
  • Relationship between arrays and pointers.
  • Array of pointers.
  • Command line arguments
  • Reading and writing with files, both text and binary.
  • Recursion - how it works, recursion vs iteration in depth discussion - Towers of Hanoi
  • Various string utilities - sprintf, strtok and many others
  • Function pointers
  • bitwise operators in C programming.
English [Auto] Hello again I'm Shivaji and I'm helping you to learn each and every corner of C programming in this section we're going to talk about storage classes. Now usually by a degree or a variable within a function we do not mention anything else apart from that data type. And the variable name that destroys class is always there when you do not mention the storage class by default the storage class is taken as auto a all. So there are four different types of storage classes auto static externe and resistor will get to know about each of them. Fundamentally what is sort of class does it defines greetings for a particular variable number one scope number two that lifetime the variable and number three the initial value for the variable is when we declare a variable along with the storage class we actually try to define these three things Scotsman's from which you reach the valuable or the variable is accessible. That means from where I can access the variable was the domain of accessibility. The lifetime means how long the variable is going to remain allocated in the primary memory. And then you feel that the value means when the variable is allocated What is the value that the variable is going to contain and doing the allocation immediately after that location. Is there any specific value that the variable is going to continue mediately after location. So these three things scope lifetime and the initial value is actually defined by the storage class. Let's dig deeper with that example. Now in this example we have declared two functions. And just one as you can see. Let us first go with the best you can see that within this we have a variable named Fed. And I have just printed the variable immediately after declaring the variable that and then I have incremented the belt loop there by one then Amec printing the content of that. Now calling the function test from main. So can you predict what's going to be the output here. At this line as you can see that I had mentioned that that videotape only I had not mentioned anything else but we do not mention anything. Then by default the storage class of the variable is taken as auto as I have already told you. It's actually the auto storage class compiler will prefix this auto with my declaration. Now the significance of all those stories is that it's not going to initialize a variable with some predefined value. There is no fixed value that the variable is going to get to when the location time or immediately after the location. So that means when I'm printing the auto variable here the bad here it could be anything I can. I cannot be hundred percent sure that this is the data that the bell is going to have. Immediately after allocation that cannot be predicted it could be anything. Now here I am incrementing the value of the that means and just adding one with the garbage value. Since we cannot predict the value so we can term it as a garbage value it could be anything. Then again printing the variables that let us just run this and see what happens. You can see that it's 0 and 1. So you will say that it's a zero. But can you guarantee that it's going to be zero. Or Louis if it is although we cannot guarantee that it's going to be zero. Always. Let me just show you this anomaly. I'm calling this one non-killing test one first of all these phunk function will be called. This is just one and we had a variable named K and again k is declared as often as we have not mentioned any storage class with this. So compiler will declare it as auto or you can just mention auto by yourself. It doesn't make any difference OK. So K will be printed and then I'm assigning 20 to k then I'm printing K here again. So K will be definitely 20 here. On completion of the call the will get back here in Maine and again in the next line. It will call the function test. Now again the variable that will be located in the memory. So in this case in the earlier case we got zero here while printing that. Let's see what happens this time. You can see that it's close to zero. It's coming from this print. And then it was to 20 here. That is from this print. After assigning credit to k we are getting 20 here now and on completing this function test one. And the control gets back to me. And in the very next line that line number 20 again we are calling this test function. So here now when the available bodies are located hourly It was zero. Now you can see that it's printing 20 on the screen. We are getting 20 so he cannot guarantee that what is the initial value of the variable it's going to be when we are declaring the variable within a function as auto. So the initial value of a variable that is having storage plus as auto is undefined unpredictable. That's why you said that is garbage. What is the scope of the activate when you declare it an auto variable within a function then it cannot access the variable outside of the scope of the function. That means we have declared this variable there. The variable that is accessible are all we can Manipal the variable that from within this function only within this opening calibers and closing callipers. You just come and go and declare. And when I have something like this here and there plus plus it's illegal compilable just as you can see that is a red line and so company really just flagger and you cannot even have this variable bag here as well in Maine with some statements like this. That's also error. So when we declare a variable as auto variable the scope of the variable is limited within the function only Skok means from where we can we can we can access the valuable other from where the variable is visible or accessible. OK so the scope of the other variable is local. It said that it's locally available within the function. Now the other thing the lifetime that's again important with the storage class whenever we are declaring a variable within a function as valuable when the function is called then the variable is allocated and when the function gets over that means when the function is done we are coming here at the closing bracket after executing all the instructions of the function. The variable gets D.L. look at it from that memory. So the lifetime of the variable which is auto is equal to the lifetime of the function. That means as long as the function is executing the variable remains allocated and when the function is over the variable is deallocated. So this is that these are the three properties of all the valuable audio storage class. That means the scope is local. The lifetime is equal to the function and the initial value for the variable which is auto is undefined. Garbage. One more thing that I would like to share with you at this juncture is that why the variable that is assigned with 20 this time. Is there any connection between the initialization of k with 20 and the value of this vat. Is there any interconnection between that or not. Yes there is an interconnection and for that you need to know how the local variables are managed within the memory. Actually the operating system creates a stack in order to manage the local variables the allocation of local variables are done in a portion of the memory that we call the stack early on the memory. There are broadly speaking two parts the stack area and the heap area. I'll be discussing about the hip area also later on when we will be having the father process. OK. So when we are having local variables that is a variable declared within a function as Ortal then these variables are typically located in the stack area of the memory. So we can just consider this as the memory that means our ram. OK so then we can just take a portion as the stack area. And what happens whenever we are calling this function just one. The operating system is allocating this one variable according to the instructions generated by the compiler K is allocated 4 bytes will be allocated for case. This is the area of K for bytes and operating system put a mark here that up to this point it is allocated so when the function is called the variable is a look at X that that's the property of the local variable are available now the evil is alive and we go with the code. Now when the function is over this variable is deallocated. Now how the location takes place. Actually the operating system just uses the mark and goes to the previous to this. Now when the mark marker is taken back to the top then what happens this portion is actually become free. Now this way although that value of k was 20 and this was kept at this location but the variable k becomes free on this location becomes free as soon as the function goes over and the market is actually moved about. So this is because this become free. Now when we get back to the main and again we are calling this function test immediately after the Net gain we are requesting for a variable there in the middle. So the operating system is going to look at for Barragan. So again the market is moved here and it is a look at it and the variable now this time is not quite there. But remember that our earlier case went up to one of what's called Clady was kept at this place so that 2:18 remained thear in in the bitis so. So why print that this time we get 20. So we cannot predict but doing the allocation or to look at an operating system doesn't take care about the internal value of the bytes that we get here. So it could be anything and that means if the program goes in with the execution and some there is some memory area was reserved and some values were kept there it did. So that's why we're getting 20 here because our idea was that look at it and the same position it was to look at it. But the value remained there. I hope that you have understood this clearly. Okay. In the next tutorial and go to tell you about that static storage class. Thank you very much.