Hey, guys, what's up?
So today we are going to learn one more concept called global variable.
So what are global labor?
So until now, we have only started local variables.
So what are local variables?
So when I ride in demesne and and I ride an X, so X is a local variable because its scope is only this.
I cannot access X here.
For example, see out X and you give me at it.
So local variables are those who are bounded by scope, whose existence is bounded by the scope.
For example, if you create any variable in a loop also then also for example.
And anyway.
So this is local variable because it's the scope is limited.
Now there was a study global variable.
So the scope of global variable is basically its its core base from the starting of the program to the
end of the program.
OK, so it can be accessed anywhere.
For example.
So how to declare that this is a global variable, so I will write in X and then I will write into Mean.
OK, so now here X is a global variable, X is a global variable.
Why?
Because it is not bounded by any curly brackets.
OK.
So it will exist everywhere.
And suppose you want to, let's say the value of access to all the global variable and the value of
global variable is done.
Now I am writing and X is close to 10.
So will this give me either.
No.
Why?
Because this X and this X, they are different.
Why?
Because their scope are different.
X is a local variable.
Whereas this is a global variable.
So if I write C out X so virtual printer then tension with printer or to will be printed.
So answer is 10 will be my output.
So for accessing global variable, I will write see out scope resolution operator and then X.
So this is called Scope Resolution
Operator.
Now if you will print this, then your output will be equal.
OK, so you have to tell the compiler that this I am trying to access a global variable, otherwise
there may be conflict like this.
OK.
So if you write normally it will be a local variable.
And if you want to access the global variable, we will use scope resolution.
Operator.
OK.
You can also change the way of global variable, like X equals X plus two.
OK.
You can also change the value of global variable.
So let's see.
So let's name this as global Nazi BP, so global
Nazi BP.
Now we will right where they were.
We asked China to allow.
So first of all, make a global variable, so and X equals 10, let's say 20.
So X is a global variable here.
Now let us create one local variable.
So and X equals 10.
OK, so this X is a local variable.
And this X is a global variable.
So this is global mediæval.
And this is my local variable.
OK.
Now, if you were to right, she yelled X.
So what should be my output?
Let's see.
So I'm out for this coming out to.
Then why?
Because local variable value is getting printer.
Now, if you want to access global variable, we're doing rate scope resolution operator.
Now let's test.
So now it is.
Doing OK, so this is global variable.
Now, what if I remove this X now that does not allow.
So global variable is getting printed.
OK.
So what if I do?
What if I remove this scope resolution operator?
So what will happen?
So there are two possible things that can happen.
So first will be rendered.
Second, the come by level.
Show us error at this lane.
So let's see what will happen.
So Tony is getting printed.
So I told you that if we want to access a global variable, we will write school resolution operator
before the name of the very well.
But here I have not written the school resolution operator.
So why it is working.
Because compilation starts from top and it will go towards boredom.
So when my compiler will reach line number 10, it already know that there is a variable that is existing
at line number four.
So it will not show us at it.
OK.
So when compiler will reach nine number 10, it knows that there is a variable called X, which is present
already.
So it will not give us error.
Hence my output will be full.
So this is hours work.
So compiler is smart enough.
OK.
Now when I was having 10 here.
So to differentiate between this local variable and this a global variable, then I used the scope resolution
operator propelled the compiler.
Do not get confused between this and the guess I want.
This means I want a global.
Don't show me the value of local.
OK.
You can also change.
You can also change the value of X, for example.
Let there does make X equals 20.
So now 20 will be my outward.
OK, so today's getting printer and hair access a global variable.
So next, we knew we were loo.
When exercise to get a better understanding of global and local variables.
OK, so that is it for this video.
Thank you.