 Hello everyone, myself Mr. F. R. Syed, I work as an assistant professor in CSC department at Walsh and Institute of Technology, Solapur. The topic for my today's lecture is automatic and register storage classes in C language. The learning outcome at the end of the session, the students will be able to describe the automatic and the register storage classes in C language. We will see what are these storage classes in C language. Let us try to understand whenever we declare a variable in C language, what is the data type associated with that variable when we declare it and also one important thing that we need to take into consideration is the storage class that is being used for storing that variable. Now, the most important thing is if the programmer fails to specify the storage class, there has to be some default storage class provided for that variable. Now an important thing also to take into consideration is where is the variable value being stored? Now whether that variable value can be stored in memory or we have one more option that the variable value can be stored in some CPU register. Now what does the storage class of a variable tell us? It gives us the following four values. The first one, the location. I mean where exactly is the variable being assigned memory or I can say where exactly the variable value will be stored, that location. Second, the initial value of the variable meaning when the variable is declared, what will be the initial value stored in the variable before the user assigns some other value. Third, the scope of the variable. The scope of the variable tells where in the program can the variable be accessed. And fourth one, the life of a variable that tells how long does the variable's value be used in the program. Now we have total four storage classes in C language. The first one, the automatic storage class to which we use the keyword auto. And the register storage class to which we use the keyword register. Third, the static storage class to which we use the keyword static. And fourth one, the external storage class to which we use the external keyword. Of course all these keywords will be in the lower case in C language. Now let us first see that if a variable is declared in automatic storage class, what are its features? A variable that is declared to be of the auto storage class, its storage will be memory meaning it will be assigned some location in memory. What will be the default initial value? It will be a garbage value that is totally an unpredicted value. Third one, what will be the scope? The scope will be local to the block meaning in whichever block that variable is declared. So it will be accessed only up to that block. Outside that block that variable is unknown. And finally, what will be the life of the variable? The life of the variable will be till the control remains within the block. Meaning as long as the control of execution is within that block, that variable can be accessed or it can be used. As soon as the control leaves that block, the variable cannot be accessed. Now let us take a sample program as you can see. So we have first declared the header files, stdio.sh. Then inside the main, we have declared two variables. That is auto int a comma b meaning this a comma b variables are of the data type integer and of the storage class auto. So if suppose I tried to print that values of the variables a and b, as in this way, a equal to and b equal to if I want to display that. So what can be the output? So let us see the output will be a totally garbage value inside it. So this is what will be the output. So these values 1637 and 9,345 actually have not been used in the program. But of course these could be some garbage values maybe on some other machine if I could do or if I could execute the program at some other instance, maybe it could give some other garbage value. So now depending upon what different values garbage values we have getting, we could say that these values could also be unpredictable. So therefore care needs to be taken in that case. And that actually forces us to initialize the automatic variables. One more thing to avoid such uncredited results, the user has to initialize the variables and of course the keyword that is used as I've already told it is auto and not automatic. Automatic is the name of the storage class, auto is the keyword being used for that purpose. So let us take one more example as a sample program. So as you can see here a k variable is declared whose value is initialized to 5 and of course its storage class is auto. And if suppose I take inside another block indicated with the help of this curly braces, I want to display the values inside this innermost block and then just outside that innermost block. Value of the variable could be 5 in both the cases because as we know once initialized the same value could be used. So that will actually give us the scope and the life of the variable. In the output we have the values as 5 and 5 for the two different print of statements. We have already seen the print of statements that we had for the two times displaying the value of k. Those were within that outermost block and as you can see the scope of k was also demonstrated meaning whenever we wanted to display the value of k. So the variable k was within that a scope of where it was declared. So as you can see control once it goes out of the block, the variable value will get lost. So now we have a question for the students now. So students are expected to think and write the answer to the following question. Now to the left we have a program so the students are expected to predict the output of the program. So pause the video and write your answer. So the answer is 3, 2 and 1 because when the control of execution reaches this first print of statements, we can see that out of these three m variables declared in the same program, this value of m that is equal to 3 will get displayed first because when this print of line is executing what we can say is this m variable has the highest priority because it is the innermost variable m that will be used and accordingly it will display the value of 3. Just when control of execution reaches the second print of statement, there we can say that this m variable that was declared in the innermost block is not accessible over here. So which are the variables m accessible? It will be this m whose value is initialized to 2 and as well as this m whose value is equal to 1. But what we can say is this m whose value is equal to 2 that will be given priority or that will be given preference. So therefore this value that is displayed with the help of print of statement will be the value of 2. So therefore you can see the second value displayed will be 2 and when the control of execution comes reaches this line of print of statement the last in this main function. So what we can say is here only this first m equal to 1 this variable m will be accessible over here the remaining 2 will not be accessible. So therefore it displays the value of m equal to 1 over here. So that is why you get the output of this man. So now let us take the next storage class that is the register storage class. So let us see what are the features of the register storage class. Of course the features of the variable that is declared to be of the register storage class the storage assigned to those variable will be of course the CPU of course not memory. The default initial value in this case also will be a garbage value that is totally an unpredicted value. The scope will be of course local to the block in which the variable has been defined and finally the life will be as long as the control remains within the block that variable can be accessed or its value can be used. So what are the advantages? The advantages would be the variable that is declared to be of the register storage class it could be accessed faster since it is declared in the register it is very much nearer to the processor. So therefore the variables could be accessed faster. And we generally make use of register storage class to be declared to those variables which are most probably used in the program or which are used to a maximum extent in the program. So for those only we declare the register storage class. So the example could be the loop counter which are generally used in a program. Then we have one sample program where we have declared a P variable to be of the storage class register and of the data type integer. So when we try to execute this loop and when we try to see what values are displayed so what we can say is it is the similar one like a normal integer variable. So the output for this program could be as expected 1, 2, 3, 4 and 5. Now some important things to be noted are whenever we declare a variable using the register storage class value may or may not be stored in the CPU register. Now there could be some reasons let us check those reasons. The number of registers on a machine or a particular register may not be available for a variable to hold value. So that could be one of the reason. Another reason could be the register may be doing some other task. So probably the register may not be available. So in that case the variable works as if the storage class is auto. I mean even though we have declared the storage class as register for a particular variable if it is not available the storage class default could be the auto storage class. One more thing we could be unable to store every type of variable in the register we have some other reasons for that as well. So let us see those reasons. The first reason is we could have microprocessor which are having 16 bit registers. So in that case when we see the float and the double data type those have respectively 4 byte and 8 byte storage requirement. So therefore the variables of such kind of data types cannot be used to store data in the register and so therefore compiler in that case treats variables to be of the auto storage class. So this is the reference used for the video lecture. Thank you.