My name is Atharva and we are continuing with this C programming course… (pause 6 sec)
In the last video we had discussed our two very excellent functions which are called, printf and scanf.
These printf and scanf functions provide us with output and input functionality.
Now we are going to see what these constants are.
Before the constants we had discussed in the last video, we had discussed our variables.
Which means, what are the variables? Any such area or part in which we are storing any value, and its value will change in our program going ahead.
These are called variables.
Constants are also similar.
A value is also getting stored in it.
But with the word constant, we understand that, this value will remain constant in the entire program.
How did it used to happen in variable? As and when we are going ahead in our program.
We were changing the variable’s inside value.
Like we put 5 in RES, going ahead in the program the RES value used to become even 8.
But when we are making the constants, once we define the value which means if we say that in RES, 5 will come.
So, in RES throughout the program, it will remain as 5.
Going ahead, it will not be 6 or 4.
Even if we want it, it will not happen.
So, who provides this functionality? These are provided by the constants.
We will see how the constants are written, and what they mean.
So, all these things we are going to learn in this video.
“The definition says that constants are the fixed value in the program.
Which means that we cannot change its value.”
So, this is a simple definition, still we will try to understand it.
Like in mathematics, there are different types of constants or even in physics there are different types of constants.
For example, we will take pi, the value of pi is 3.14 and this is constant, if you write in any formula, we will always write the pi’s value as 3.14.
Which means that pi is constant.
You go anywhere in the world, wherever pi will be used.
Its value will be 3.14. Exactly the same way we want such things in our program, whose value we don’t want to change anytime going ahead in the program.
Suppose we take an example.
Our user has to know the volume of 3D figuRES like spear, cone or cuboid.
This kind of 3D figuRES.
And the volume is calculated differently through different formulas for different shapes.
So, in the same way, we have to make a program with the help of which the user can calculate the volumes of different 3D shapes.
Like if we take a spear, whatever will be the volume of the spear, we will make the formula for that volume in our program, and we will insert that value and then we will get the volume of that particular spear in our output.
Now the formula of this volume, we can see in it that there is a value of pi in it as well.
Which is always constant, we spoke about that.
Its value will be 3.14.
What will we do about this 3.14? We will define it in the variable, like we store the value in any variable.
So, when we want to use that value, we call the variable.
Like we can see here in the example, in RES we have stored 5.
So, whenever in our program we would write or type RES.
We will get to know that the value inside it is 5.
So, in the same way, if we want to make this type of program where we are finding the volume of different shapes like spears.
At that time, we have to keep the value of pi constant.
If going ahead in the program, if by mistake we define such a line which will make our pi value as 4.6.
So, will our volume answer be correct? No, not at all.
So, in this way we have to keep 3.14 as constant.
But since we store it in variables.
If we make it a variable and store it, it is a variable's functionality that we could change the value in it.
So, if we wish that, we define such a thing in our program whose value remains constant throughout.
At that time we make constants.
Now, there is an easy way to make constants.
We will see how we can make constants.
But for now, the example in front of us, we will read it.
We are seeing here that there is a variable called RES in which we have stored 5 as a value.
We understood this much that the RES was a variable and we have put 5 in it.
Before that, if we see, we have also written the data type, which type of value will go in the RES variable, integer type of value will go in it.
So, by writing INT it is clear to us that our variable will use 2 bytes of storage in our memory, it will utilize that, we had even discussed in our previous video that different data types come in different sizes.
And it defines different things.
Like integer will tell that in it only integer value can be stored.
And its memory occupation will be of only 2 bytes.
So, in this way this is our data type which we understood.
If we see ahead of our data type, we have written one more key word.
We had discussed keywords in the previous video.
keyword is a REServed word which will have a special meaning in our C programming.
This CONST that you are seeing before our data type, even this is a keyword and it has got a special meaning.
Now the RES variable that was there, in which the stored value was 5.
If we put CONST in front of it, like it has been written here, if we write it here.
So, what will happen is that, RES will become one constant.
In the entire program the RES values will be 5.
If we forcefully want to make it 6, it will still not happen.
Our program will be spoiled and there will be an error in it.
So, we will practically use it, and see how these things happen and how the error will come.
So, we will see these things but for now we have to understand how we make that variable constant, by writing CONST.
So, we will go ahead and see these constants are of how many types.
Since this is one type of variable.
Which means it is working like a variable, which is of types like integer, floating, string, character.
In the same way constants have the same types.
It can be integer, float, string or the character.
So, these are the types of the constants.
Now we will see how exactly we will make these constants.
If we have to make one variable constant, how will we make it constant? So, there are 2 ways of making it.
First is using hash define. Okay.
So, what is this hash define? This is a preprocessor command.
We had learnt before, that whichever command or whichever thing starts with a hash, we will call them preprocessors.
And this is the work of a preprocessor, that it defines a few things from the start of the program.
That is the reason it is called a preprocessor.
Before the starting of the process, pre. It defines a few things.
So, these are the keywords of hash define.
With the help of hash define preprocessor, we make our constant.
How we will make it, we will see that going ahead.
The second method of making constant, we saw it previously in the example, before our data type we wrote CONST, so that the variable also becomes a constant.
So, RES variable also became a constant because we wrote CONST in front of it.
So, these are the two things that make it constant.
Now we will understand them one by one.
The first way, hash define we will see.
This hash define preprocessor, it makes our variable constant.
You can see its example here, hash define, so this is a preprocessor command.
After that we have written one name, like it is written here salary.
We will say that salary is a type of variable in which 40000 value, as it is written, is getting stored.
If under salary 40000 is stored and if in front of it, we have written hash define preprocessor command.
With this we understand that, salary will remain 40000 in the entire program.
It will not change anywhere and it will become a constant.
This is the first way. The second way was by giving the CONST keyword.
So, here in the example we can see that in “A” variable 10 value is stored.
We wrote its data type, that is integer type.
Apart from that, we wrote a keyword CONST, which means constant.
So, “A” variable has become constant.
Now its value will be the same in the entire program.
It will not change.
Now we will go ahead and see practically, how these things work.
So, this is visual studio code with us, in which you can see that there is a text editor.
Here I have made one more file.
In the name of constants dot C.
You all must be knowing that, you have to go inside the explorer and wherever you have to make the file, clicking there, you have to go on the new file option and make the file.
So, I have already made the file.
You can see that her constants dot C is open, I will close it.
You can see that here a program is written.
Now what is written here? we will read that.
The first line is saying that hash include STDIO dot H.
Now we know that this is a preprocessor command hash include.
What this is doing is, it is importing a file which is named STDIO dot H.
And what STDIO dot H does is, it helps us in making the input and output.
So, this is the first line.
After that we will focus on the second line which is our hash define preprocessor command.
hash define, ahead of it we wrote value.
Now I want to change the value to… RES.
Suppose that I made RES a type of variable.
And in that, next we wrote as 30.
So, what has happened in RES, the value 30 is stored.
Since in front of it we had written hash define preprocessor, that is the reason RES has become a constant.
Now its value is 30, which will not change going ahead.
So, we will see if it really happens this way.
For that what we will do is, we will go in int main function.
Int main we know that is the main door of our program.
In this the main program is written.
So, we will come and first of all see, for now we will remove this.
We will see it later, we only have a printf function statement, and in that we have written percent D in double quotes, and then by putting a comma we have written a variable’s name RES.
Which we have defined earlier with the help of hash define.
So, now you can see the flow of the program.
With hash define we have made a constant.
Whose value would be 30.
In the main function, we have printed the same 30, with the help of printf function.
And we have basically printed RES.
So, we will see how this program works.
There is white dot coming over it, which means this is not saved.
So, I will save it with Ctrl plus S.
And I will right click and click on the run code.
As soon as we click on the run code, you can see that 30 value is printed.
Which means in RES we had put 30, and when we printed RES its value remained as 30.
Now we will try one thing and see if it works for real or not.
I will come here and I will write here.
We want to make RES not 30 but 40.
You can see that, as soon as I wrote it, a red light came on.
Red line means, there is some problem with your program.
Means there is some line which is not making any sense according to the compiler.
But still, we will save it.
We will run it and see what exactly this program does.
So, we will see that no output has come.
Here one error has come.
You can see that, in red colour, an error has been written.
And the error is saying, you have written RES is equal to 40, there is a problem with this sentence.
Now we will see up, we have made RES constant.
So, how can I make it 40 going ahead in the program? When it is already defined as 30.
So, what I will do is, I will remove it, and I will get to know that I have this as a constant.
So, I cannot change its value.
I will run the code again and I will see 30 getting printed over here.
Now we will use the other way.
We will remove this hash define thing, and inside it, we will write CONST.
We will make one integer type variable QWE or we will keep it as RES.
But we will change its value to 78.
We will put a semicolon.
Now you can see, even this we have made it a constant.
In the previous slides we had seen, how we will write data type of RES, supposedly if it is a data type.
And before that we will write CONST, and tell it that this is even a constant.
As soon as we wrote it, this 78 got stored in RES and became a constant.
Now I will save it and print it, and see what the output will come.
You can see that 78 has been printed.
Which means our output is taking 78, from the constant, which means from RES it is taking 78 and getting it printed.
How will we confirm that it has become constant? Its value will not be changed.
So, we can see it here.
Here I will write RES.
And going ahead I will try to write 34, or let's write 345, and I have put a semicolon.
Now again it has again become red.
It has become red means there is some problem in the code or the program.
I will do Ctrl plus S, still I will see what will happen after that.
Because we have made the RES constant already.
But I am going ahead and changing its value.
So, what is the output I will get in my console? So, you can see that I have again got the error.
And in the error, it is telling me that in RES, the 345 that you have written, there is a problem in that.
If we see up, this was constant.
We cannot change the value of RES.
That is the reason why this error is coming.
So, in this way we can make our constants.
Again, we will remove this RES quickly.
I will press backspace and save it and I will run it.
Now you can see that again 78 is getting printed here, that means that we could make constants with the help of hash define and apart from that the const keyword, with its help.
With the help of these both methods we can make constants.
So, whenever any operations have to be done, in which the constants are used, it can happen very easily and we don’t have to think that by mistake its value should not change in future.
So, in this way the constant is needed, and with the help of constant we can make different programs.
If you think that there is any part of this video or this topic, you might have not understood then, 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 queries.
Very happy 😄😁
Very happy and greatful, thank you , great course.
Super for poor students to learn software course languages TQ