 Hello everyone. The topic for my today's lecture is Static and External Storage Classes in C language. In the previous lecture, we have seen the automatic and register storage classes. Now, this time we will see the remaining two storage classes. Now, the learning outcome at the end of the session the students will be able to describe the static and the external storage classes in C language. Let us first see what are the features of a variable if it has been declared with the static storage class. Now, the storage is memory meaning if a variable has been declared to be of the static storage class, it will get place in memory. The default initial value for the variable will be 0. The scope will be local to the block meaning the block in which the variable has been defined and finally, what will be the life? The life of the variable will be the variable value will persist between the different function calls meaning if a particular function has been called more than once and in that function if the variable is declared to be static that value of the static variable will be persisting that means will be remaining will be saved and that could be used between different function calls. Now, like the auto variables the static variables are also local to the block in which the variable is defined. The static variables do not disappear when the function is no longer active meaning if the control of execution has left the function still the values of the variable will be preserved that is what the values of static variables persist between different function calls. If the control returns back to the function then the static variables have the same values as the values that it had when the control of execution has left that function. So, this is what we have as a program a sample program where you can see on the left a variable i is declared to be of the storage class auto whereas, to the right the variable i has been declared with the storage class static. So, when we compare in both the cases the increment function has been called 3 times from the main function and when we see the code of the increment function to the left we are going to print the value of i and every time when we are going to print we are going to increment the value. So, when you see for left hand side the i variable when it has value 1. So, first time when it is called it displays the value 1 next time before returning the execution control to the main function it tries to increment the value of i with the help of the statement i equal to i plus 1, but when you see next time when once again the control of execution comes to this function with the second call to the increment function the value of i is still 1 and the same is the case with the third call. So, that means, the value of i every time when it calls the increment function and it tries to increment the value the incremented value does not get stored, but when you see in the case of the static the static int i equal to 1 is the declaration of the variable i in the function increment it displays the value of i and then it increments the value. So, when it returns the control to main function and when it calls the increment function for the second time. So, as we can see the value displayed is 2 and next time the value displayed is 3 means what in between the different calls to this increment function the i value when it is incremented it is get stored or it is get saved. So, that means, the value persists that means, the value is saved between different function calls and it can be later reused the variables do not get destroyed. So, when variable i is auto it is reinitialized to 1 every time it is called on function termination the value of i vanishes and the new value of 2 was lost. Now, this what we are seeing is in case of the auto variable. So, whatever may be the number of times the function is called i is every time initialized to 1, but this is not the case with static function when it is initialized to 1 only once at the first time when the function is called and since i is static the value is persisting and next time when it is called i is not reinitialized to 1, but the value of i which was there for the previous function call whatever it has changed. So, that value will be used. So, therefore, we see that the old value of 2 was available therefore, it was taken and displayed and next time it displays value of i as 3. So, therefore, the value persists between different function calls. So, one precaution that we need to take is avoid using static variables quite often. So, only use them when it is really needed as the values are kept in memory it could be reason that they take up the space in memory. So, that memory will not be available for use by other program. So, that is what care needs to be taken while making use of the static storage class. And if that variable is holding that memory probably that memory location cannot be assigned to other variable of other program. So, now we have a question for the students. So, students are expected to think and write the answer to the following question what is the output of the code. So, pause the video and write your answer. Okay. So, what is the output of the program the output of the program is as you can see in the right section 0 2 and 3. Now how did that output come we will see it the execution control starts from the main function as you can see m is initialized to 2 and then the function fun is called for the first time and fun function is called you can see that variable m is declared to be of data type integer and of the storage class static. So, therefore, m value is initialized to 0 as we know static variables are assigned values 0 at initialization or at the time of declaration. So, the first time the value of a variable m is displayed using this printf statement. So, therefore, we see value 0 being displayed. Then the value of m is incremented to 3 when the control of execution returns back we can see over here there is one more statement of printf inside main function, but here we are using the variable m. So, what we can say is m over here the value of m is 2. So, therefore, that value will get displayed over here. So, therefore, second line displayed is 2. And when once again function fun is called at that time here the variable m is not once again declared but what we can say is the previous value of m which was 3. As you can see the last time when we had left this function we had incremented the value of m by 3. So, that value of 3 0 plus 3 will be displayed over here. So, therefore, the third line displays the value of m as 3 and therefore, it returns a control of execution back to the main function. That is why you get the output as 0, 2 and 3. So, now let us take the final storage class that is the fourth storage class external storage class. Now, let us see what are the features of a variable that is declared to be of external storage class. First is the storage. The storage assigned to a variable of the external storage class is of course memory. Then the default initial value for that variable will be 0. Then what is the scope? Scope will be global meaning whenever a variable is declared to be of the external storage class the value that it has will be used globally. That means all the functions declared within a program can access this variable value. So, therefore, the scope is global, but what we have seen for the previous 3 classes the scope was local. And finally, what is the life? The life is as long as the program executes. So, therefore, scope is global and not local meaning anywhere in the program whatever functions you declare whatever functions you use in those functions you can make use of this value of the variable. So, therefore, it is a global scope. What variable we are using with the help of the external storage class? Since it is declared outside all the functions and still it is available to the functions in which it has not been declared. So, there could be chances that sometimes the same variable name is used for a global variable as well as for a variable that is inside a function. So, if a function is making use of a variable name that is of course used for a global variable. So, in that case the preference will be given to that local variable. So, now we have a sample program for this case. We have declared an i variable an increment function where i value is incremented by one and then it is going to display after incrementation what is the value. So, now here we have the main function. So, let us see how does it execute. So, first of all the value of i is going to print. Since i is declared as a global variable whatever what I can say is in whichever function we are going to make use of i if a variable i is not declared in that function therefore the global variable will be used. So, here we can say since it is a global variable an external variable its value is initialized to 0. So, therefore I can say in this printf statement i value equal to 0 will be displayed then the increment function is called where the value of i is incremented and let us see what the incremented value will be probably it will be 1. So, the output could be 0 and 1. Let us see the output. Yes, before incrementation i was 0 and after incrementing the i value has now become equal to 1. So, we have one more sample program where we have declared an x variable equal to 21. So, now we can see in the main function we are specifically making use of the keyword extern to declare the variable y of the data type integer. So, why we are making use of extern over here is because we know that in the next line when we are going to make use of the printf statement to display some values we are displaying the values of x and y and suppose if we do not declare y variable over here. So, probably it could raise an error because you can see after the main function we are declaring the y variable to be of the value 31 and data type integer. So, the output of this program is not given in this slide or in the subsequent slide. So, students can just guess what the output of this printf line could be. So, I will take the next example and next sample program. So, here we are declaring x variable with initial value 10 then in the main function we are declaring another variable x with initialization value 20. So, then we are going to print that value. So, let us check what the output could be the output in this case will be first of all this x value will be displayed over here. Now, since there is a situation where the same variable name has been used for the global and local variables. So, here preference will be given to the local variable. So, therefore 20 value will be displayed then the display function will be called and in this display function the x variable value is displayed. Now, here in the display function there is no x variable declared. So, therefore it takes the global variable x and it displayed its value as 10. So, that is what you have as output 20 and 10. So, now whenever we declare variables our main aim is to economize the memory space consumed by the variables and to improve the execution speed of the program. Meaning we need to make sure that we use memory in an optimum way. Now which storage class should be used for which variable that we will see in the next few points. So, a variable will be declared as static only if the value needs to persist between different function calls. In that case we will make use of the static storage class. Then a variable will be declared to be of the registered storage class only when we have a situation where the variables will be used often in a program. So, in that case we can make use of the registered storage class. The external storage class can be used for those variables which are almost used by all functions in the program. And finally, we will make use of the auto storage class if the top 3 are not the situations in a program. So, we do not have such restrictions in a program. In that case we can use any of the auto storage class otherwise. So, this is the reference used for the video lecture. Thank you.