Scope of Variables – Local vs Global
today our topic of concern is to study scope of variables. outline
of today 's lecture is to define scope of a variable local variable
and global variable. we are going to understand what is local
variable and global variable. what is scope . scope is nothing but
a lifetime or you can say lifetime of a variable. contents of this
block up to this point are visible to the internal block. contents of
internal block are not visible to outer block. therefore it will not
be able to print this variable you can say. and it will produce an
error which is variable undeclared. that is what we call as a
scope. this is called redefinition of a variable. this is something
which is not allowed at all. you are not allowed to declare or
define another variable of same name within the same block. and
this is something similar to what we have seen over here. if you
want to define two variables with same name but in different
blocks, then it is possible.
We are going to have a lot of discussion on function prototypes.
right now, you wo n't need to bother about it at all. let 's try to
understand this code snippet which is available over here.. here I
have defined a prototype of a function. here you wont get any
error because you are not redefining within same block but within
different blocks. there is no local variable inside this function..
compiler will look for a global variable and it will print the
contents of this global variable.. This is what we call the use of
global variable. thank you for your knowledge of how to use
global variable in your work..
today our topic of concern is to study scope of variables. outline
of today 's lecture is to define scope of a variable local variable
and global variable. we are going to understand what is local
variable and global variable. what is scope . scope is nothing but
a lifetime or you can say lifetime of a variable. contents of this
block up to this point are visible to the internal block. contents of
internal block are not visible to outer block. therefore it will not
be able to print this variable you can say. and it will produce an
error which is variable undeclared. that is what we call as a
scope. this is called redefinition of a variable. this is something
which is not allowed at all. you are not allowed to declare or
define another variable of same name within the same block. and
this is something similar to what we have seen over here. if you
want to define two variables with same name but in different
blocks, then it is possible.
We are going to have a lot of discussion on function prototypes.
right now, you wo n't need to bother about it at all. let 's try to
understand this code snippet which is available over here.. here I
have defined a prototype of a function. here you wont get any
error because you are not redefining within same block but within
different blocks. there is no local variable inside this function..
compiler will look for a global variable and it will print the
contents of this global variable.. This is what we call the use of
global variable. thank you for your knowledge of how to use
global variable in your work..