Hello everyone, my name is (name) and we are continuing this C programming course…. (pause 6 sec)
In the last videos, we had discussed variables and constants.
We saw how exactly a variable is declared, and how it is made, and while declaring it what is the information, we are giving to our compiler, with the help of which the compiler gets to know what size he has to allocate to the variable, in which its value can be stored.
All this information, we give at the time of variable declaration to our compiler.
In this video, the topic that we are going to discuss is called “storage class”.
Storage Class is a kind of information, which we pass at the time of variable declaration, exactly like a data type with which our compiler gets some additional information, about a variable, like what would be the exact life of the variable or where it will exist, etc.
The information that we are giving apart from the data type, that we call a storage class.
In this video, we will be seeing all these things, how the storage class works, where we have to declare it, where we have to write it and what is the need to use it, when we know that we are already using data type at the time of variable declaration.
So, what is the need now to write storage classes separately, at the time of variable declaration. So, we will be seeing all the answers to these questions in this video.
So, without any delay, let's start with the first question, what is the storage class? Let's search for this answer.
If I talk about this question, we will first of all start with the definition, because the definition brings the entire concept within one or two lines.
If you talk about the definition.
The definition says, storage class defines the scope which means visibility and lifetime of a variable within a C program.
Means what is this definition trying to say is the variable in C program, its scope, which means from where to where it is available, its availability, its visibility, all these things or its lifetime, which means after which area or after which part of the code we will not be able to use it or its value we will not be able to access.
2:40
All these things we are able to know with storage class.
We will try to understand it once again.
Like we declare the variable time, we write a data type related to the variable in which we tell what are the values stored in the variable, like is it an integer, float, character or which type of value has been stored.
This information we give to our compiler with the help of a data type.
But if I want to give such information, like my variable will be available from where to where, from where will I be able to access the value and from where I won't be able to access.
if I want to give this information to my compiler, at that time I write my storage classes, during variable declaration.
So, this is the thing exactly when the storage class is written and why is it written? Now we will quickly see its syntax, so below you can see that we have made a variable called r-e-s in which we are storing value 5.
If we see before r e s name, we've already seen that we have written an integer type of data type with which we get to know that res is an integer type variable and it will handle only the integer values.
So, this thing is clear with our variable name and data type.
But if you come ahead of the data type in this particular syntax, you can see that with the auto name we have written something, this auto is basically the storage class in the same place we are writing the storage class related to any variable.
The storage classes are of different types.
These are of which types? all these things we will see.
Before that you have to understand that before the data type, we write a storage class at the time of the variable declaration.
Because of which some additional information reaches the compiler related to the lifetime of the variable.
Now, we must be thinking that if we are learning the storage classes now.
Then till now the variables which we were making with the help of only data types, were not the storage classes made? This information was not getting passed on to our compiler about how much would be a variable’s life, where we’ll be able to access it and where we will not be able to access it.
If I talk about that thing or that situation where we were not writing this storage class, in the previous examples when we were making the variables.
At that time a default storage class is made already and the compiler makes it during every variable declaration.
And what happens with it is, by default the storage class that is made because of that few sets of rules get applied for our variable and on that basis our variable goes on.
5:30
So, which is the default storage class or how many types of storage class are there.
This we will discuss in the next slide.
We understood what a storage class is.
Now we will quickly see what the different types of storage classes are.
We have seen that there are four types of storage classes that have been listed.
From which one is auto, in the previous slide we had also seen one syntax of this, before the data type, a key word, auto was written. auto is one type of a storage class.
Apart from auto storage class there is one register storage class, apart from that there is a static storage class and there is also extern storage class.
So, these are the four types of storage classes.
We will see them one by one and will understand the exact differences between all of them and why we use them separately.
Which means that in this we will know that this auto or register, how much life it is giving to our variable, in that variable by default which value is stored.
So, all these things we will see in the coming slides, one by one for every storage class.
We will go ahead and see the first storage class which is an auto storage class.
Let’s gain some information about it.
We will start with the definition.
so The auto storage class is the default storage class for all local variables.
This definition is extremely simple.
Where we were talking about the situation, till now in the variable declaration we were not writing any storage class.
So, when we were not writing any storage class, at that time the default storage class that was getting added was our auto storage class.
So, by default, the storage class that gets linked to our variable is called an auto storage class.
What does this auto storage class define? What does this auto storage class do? In any particular part of the program, this variable we had made.
Till that part itself this variable is limited, if I talk about the part of the program.
Here I'm talking exactly about the curly brackets which divide parts in any of the programs, like our main function always starts with curly brackets and it ends with the curly brackets.
So, this is the part of the main function, in that if you see ahead when we make functions, the limit of the function is also defined with the curly brackets.
If I say that I'm talking about the part of the program where our variable is declared, so the part of the program is basically those curly brackets which is defining our range, our part and our body.
We will see more information about it as and when we will go inside the program.
But for now, we have to understand that our auto storage class which is made by default at the time of variable declaration, what does that do? In whichever part we made that variable, it has access to only that part, which means if in that part wherever you will use the variable, access its value, you will be able to access it very easily.
8:45
But if you go outside of it and you want its value outside as well, that time the auto storage class will not allow it and you will not be able to access that value.
Now we will understand it quickly with a basic example here.
You can see below we have declared two variables.
In the first variable we have only given the data type and in the second variable which has the name ‘days’ in that we have given the data type plus the storage class.
Here the storage class is auto storage class.
And if I talk about by default storage class, here both the variables are declared in the same way where we have not written any storage class in front of the month.
still At that time auto storage class by default joined with the variable and its rules were applied on the month variable.
In the same way the month variable as well as days variable have been declared in the same way.
And there is no difference in both of them.
Why? Because, the auto keyword, which is there, the auto storage class, even if we don't write it, by default that storage class would have been added.
And if we wish to write it, it is up to us.
With this it stays in our mind that I have defined this variable in auto storage class.
10:05
So, this was about our default storage class or the auto storage class, now we will go ahead and quickly explore one more type of storage class.
And the name of this storage class is ‘register storage class'.
If I talk about definition here, when we want a variable to load faster, we declare it in the register storage class.
So, if I want to use such a variable that I’ll be using very frequently and I want that variable to be very fast.
I should get the value inside it very fast, if we are making normal small programmes then the speed doesn't matter here.
But when we are writing big programmes and we are making big applications from our programmes, at that time the lines of code are many and the time that is required to process those is very important, that is the reason we wish to have such variables which are very fast and should save our time as we have to use those variables quickly and frequently.
So, those variables we store inside register storage class which means we declare it under register storage class.
Why do we do it? We will see it.
Because we declare it in the register storage class.
What happens at that time is, sometimes it creates the variable in the CPU register.
So, till now when we were making the variable, we were declaring it, at that time the location was getting allocated was in a normal hard disk and in those, values used to get stored.
But when I talk about the register storage class.
So, there is a register in our CPU which has a very small storage, but is extremely fast.
Which means, the things that you're saving inside it or you're extracting from there, the process is very fast as compared to the hard disk.
In this what happens is when you want to use such a variable which is very fast and it should extract the values very quickly.
At that time, you declare it in register storage class, since we have declared it inside the register, at that time the register inside our CPU which is a very small storage and is very fast, in that our variable gets declared and the memory gets located and in that the values goes in and out.
But this doesn't happen all the time.
It happens sometimes when there is some empty space in the CPU register with which it can make the variable, at that time only in our register these variables are made.
So, with this storage class what happens is, the variable becomes very fast as compared to normal variables.
If you see an example here, how to write a register storage class, so the syntax is absolutely the same.
You just have to change the keyword and register and your variable declaration line will be defined in register storage class.
So, this was about auto and register storage classes.
13:13
We will go ahead and see our third storage class which is a static storage class.
Now variables which need to retain their values even outside their scopes are declared under the static class.
Now what is this trying to say? We spoke about the part of the program which means the curly brackets.
What we do is when any variable is made in auto storage class.
At that time, in the block in which the variable is made, it is limited to that, its functionality works within that.
But if we have to make such variables which can be used outside as well and even after extracting and accessing it multiple times, its value should always remain the same or static.
At that time, we are using this static storage class, you might find it complex to hear and you might not be able to understand it.
There is no problem as we are going to explore it practically, how the static storage class works and in which all situations it tackles and provides the solution.
The way to write it is also very simple.
You just have to write a static keyword in front of your variable declaration and your static storage class is ready.
So, till now we have seen and declared three storage classes here.
Now we will go to our fourth storage class and we will see what exactly it is.
So, the fourth storage class is extern storage class, what this basically does is, variables which are declared in a scope or block can be used in another block are created using extern.
Extern also helps to share variables from one file to another, here when we spoke about the part of the program in which you had made a variable, you cannot use that outside.
but If you still wish to use it outside so at that time you declare it under extern storage class.
What happens in this?
Suppose you have made a really big application for which you require different C program files.
Which means your work doesn't get complete with one C program file.
So, you have made and kept multiple interlink C program files.
And what you have to do is in any one program file, the variable that you have made, you have to use the value of the same in another C program file so at that time also you can declare it under extern storage class.
What happens with this is if in any other file you have stored the value in the variable, that value you will get in another file.
We will quickly understand it practically, with that we will be clear about the things.
The syntax to write extern is also the same.
16:03
You have to write Extern as a keyword, you have to keep the integer data type and the variable that you want to extract for some other place.
You will write those variable’s name over here.
That's it.
after that you don't need to do the initialization as the initialization will be extracted from where the variable was first made.
So, this was about four storage classes.
We saw theoretically, concept wise, how these storage classes are different from each other and how they work separately from each other.
Now, it is the time to see them practically.
Now, we will quickly go into our text editor and here we will see here with the help of different programmes how these storage classes work.
This is one program related to auto storage class, where we have made three variables of different data types, without initialising them, which means without storing any values in them, we wish to print them.
When we will print them, what will exactly be printed, all those things we will be seeing here.
So, this is defined under the auto storage class because we have not written any storage class in front of it, so we know that by default storage class which means the auto storage class has got linked with it and all the variables must be following the auto storage class rules.
So, a, b and c, we have got three variables, we are printing all three here but without initialising.
We will quickly go inside our terminal and we will run our program to see how it runs.
Now it has got compiled, I’ll just run it.
So, now you can see that we have printed these three variables.
Which means we had printed a, b and c with the help of print f function.
In this printf function all three were of different data types so we used three different format specifiers. right?
So, the printf function is basically helping us to print some information on the terminal.
So, you will see here that when our information is printed, it has been printed a little differently.
Which means we are not able to understand, here you can see that zero has got printed, then on the other variable, somewhere 0.00 has got printed, somewhere nothing is printed, blank.
So, what is exactly going on here?
So, if we see here carefully, since we have neither initialised anything here, nor assigned or stored.
So, what the auto storage class does is, by default it stores a garbage value, through this the location, the allocation made of that particular storage that is filled.
Now what is the garbage value? (repeat)Any random values or any random numbers, any
values, spaces or anything which is like garbage and it's of no use to us.
This auto storage class is giving garbage value to every variable.
Till the time we don’t precede with any initialization, which means we don't store any value, till that time there will be random values stored in it, with which what will be assured is that the location that we had defined to store our variables that is safe and that location will remain there till the time we don’t put any new values in it.
After putting the new values, these garbage values will be removed and the new values will be stored in those places.
So, we saw a few rules related to auto storage class.
Now you will see that in ‘int’ main function, we have made these variables.
So, what will happen at this time?
If we keep them outside the main function.
So, this was about the auto storage classes.
We will go ahead and see our second storage class which is called register storage class.
This is a program related to register storage class and what is this basically doing.
It has taken ‘a’ variable in which we have stored 3 values and what we are doing is extracting the address of the variable, exactly on which location it is stored.
And we know that whenever we want a variable’s address, at that time we add an ampersand sign before the address because of which we are getting its address.
So, here what do we want, we have made one ‘a variable’.
And before that we have placed one ‘and’, which is called an ‘ampersand’.
What this ampersand does is, it provides us with the address of the variable’s location.
So, let’s check here if it is able to remove the address and give it to us, since we have stored it in a registered storage class, is it giving us the address here.
So, we will go inside our terminal and we will execute this particular program and check.
You can see that as soon as we compile it, it is giving me an error.
What is the error basically, error says that the variable that you had made in the register storage class, we cannot access its value, we cannot access its address.
Why can't we do it? We had seen that what the register storage class does, it makes a space in the CPU register for our variable and there it gets it stored.
21:37
So, the CPU register address cannot be accessed by our compiler.
It can only access the normal hard disk storage space, that is the reason why our compiler has generated this error which is basically saying what you have done, you had tried to access the registered storage class address, which is not possible because it is stored exactly in the CPUs register part which is out of our reach.
We are not able to access that.
This was about our register storage class.
Now we will remove this and go to our next storage class which is called a static storage class.
Here we need to pay attention, if you see here, we have made a function which is not needed to be seen now.
We will be going ahead and reading how we make a function.
For now, you skip this upper part.
You see the preprocessor command and come directly to the main function.
As soon as we come on the main function.
We can see that we have made an ‘int i’ named variable.
What this variable named int i will do is, this i named variable would change its value like 0123.
So how will it change these values? It will change the values with the help of a for loop, so you don't have to pay a lot of attention here.
What is this for loop function that we are seeing? These things we could be learning going ahead .
But what does this for loop basically do?
This for loop increases the variable’s value slowly, slowly, one after the other.
So, here we made the i variable and i variable will increase one by one from zero to three.
We have called the function here.
We have used this function here which we had made up here.
So basically, what is this program trying to say here? Since it is a complex concept to understand as there are some concepts which we are seeing for the 1st time.
Here there is no reason to create any complexities because we would see about store class in our future programmes, how are we making one static storage class.
For now, we will understand that we have made a variable in the name of a static storage class.
The variable has become static.
The video has become static means that you're using it in a different part even then its value will be the same which you had defined at the start.
If they are used somewhere else all these values will remain constant.
If there is a variable, we have made it on the top, in ‘a’ we have stored 1, in b we have stored 11.
Wherever you are accessing a and b here and you want to use it, you will get its value as 1 and 11.
Even though we can do some operation on it and increase its value, initially we'll get the values as 1 and 11 of a and b variables.
About this concept we will see in the future videos when we will get knowledge about the function and about for loop, we will get the information.
At that time, we will understand the static storage class easily.
We will go ahead and see our last storage class quickly.
What is this? Even this is a kind of a program in which we have made one variable in the extern storage class.
We had just learned that what happens in extern storage class, there is a value stored in the variable in some other place and I want that value in some other place.
At that time we use this extern storage class and here you can see in the program, this is our main function but this ‘int a is equal to 20’, we have written this outside the main function and I want this a is equal to 20.
Which means in the 20 value that is stored in ‘a’, I want to use that in the main function as well.
So, what I will do is with the help of extern storage class, I will make one variable of the same name.
With this what will happen is my compiler will search that this names variable is made in our program of which value is initialised.
He will get to know that okay, there is ‘a named variable’ and its value is 20.
So even here in a the value 20 will come and then when we will print it with the help of printf function, which means we will print our a on the terminal.
What is the output that it will give? We will quickly check it.
So, we will go to our terminal and we will compile our program….
So, we have compiled this and after that we will execute it here.
You can see that as soon as we printed a, the value is printed as 20.
So, we had made it outside of our main function but with the help of our extern storage class we could access it even in our main function.
So, this was the functionality about our external storage class.
All these storage classes we have learned and one by one we understood them practically as well.
How do they work? Here, the storage class concept gets over.
In the way we used to write the data type in our variable declaration, in front of that we can write the storage classes.
With which our compiler gets some additional information.
If you think that there is any part of this video or this topic, you might have not understood then you can ask us any query or question related to that topic without any inhibition.
If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.
Apart from this if you wish to have a discussion on any topic.
Even that you will be able to do on forums.learnvern.com.
Share a personalized message with your friends.