i re-read the previous chapters, and i caught this confusion here
A variable can be either of global or local scope. A global variable is a variable declared in the main body of the source code, outside all functions, while a local variable is one declared within the body of a function or a block.
Global variables can be referred from anywhere in the code, even inside functions, whenever it is after its declaration.
I thought the author initially said "outside all functions"
but the 2nd line he said even inside functions
Variables. Data Types.
so what does he means?
i read that from the link (scroll to the mid-body of the page)
confused about this global variables
Started by jwxie518, Feb 17 2009 06:17 PM
4 replies to this topic
#1
Posted 17 February 2009 - 06:17 PM
|
|
|
#2
Posted 17 February 2009 - 06:47 PM
Have a look at this code:
int globalint;
float globalfloat;
int test()
{
int testint=globalint; //OK, globalint is visible
int testfloat=mainfloat; //error! mainfloat is out of scope
return 0;
}
int main()
{
int mainint=5;
globalint=10; //OK, in scope
globalfloat=10.0; //OK, in scope
float mainfloat=globalfloat; //OK
testfloat=mainfloat; //error! testfloat not in scope
return test(); //OK test() is global so in scope
}
#3
Posted 17 February 2009 - 06:58 PM
from my interpretation, i read it like this
okay, we made globalint and globalfloat as gloabl varibles, so they can be use in anywhere, without re-declare again
when tesfloat is assigned to mainfloat, which hasn't yet declare, is not valid
when you assign testfloat to mainfloat, again, testfloat is no where (no declaration)
so simply you mean, local variable is anything within the block of that function body, but can only use within the block of the body
while gloabl, can be use anywhere, any block of function, in this case, can be either main, or test, or even both....
right?
if i make testfloat as a local variable, then this statement is true
if i am right everything, beside any comment, if i don't want to change the code at all, but to make testfloat (the one in test function) valid?
okay, we made globalint and globalfloat as gloabl varibles, so they can be use in anywhere, without re-declare again
when tesfloat is assigned to mainfloat, which hasn't yet declare, is not valid
int globalint;
float globalfloat;
int test()
{
int testint=globalint; //OK, globalint is visible
int testfloat=mainfloat; //error! mainfloat is out of scope
return 0;
}
int main()
{
int mainint=5;
globalint=10; //OK, in scope
globalfloat=10.0; //OK, in scope
float mainfloat=globalfloat; //OK
testfloat=mainfloat; //error! testfloat not in scope
return test(); //OK test() is global so in scope
}
when you assign testfloat to mainfloat, again, testfloat is no where (no declaration)
so simply you mean, local variable is anything within the block of that function body, but can only use within the block of the body
while gloabl, can be use anywhere, any block of function, in this case, can be either main, or test, or even both....
right?
if i make testfloat as a local variable, then this statement is true
int test()
{
int testfloat,
int testint=globalint; //OK, globalint is visible
testfloat=mainfloat; //error! mainfloat is out of scope
return 0;
}
if i am right everything, beside any comment, if i don't want to change the code at all, but to make testfloat (the one in test function) valid?
#4
Posted 17 February 2009 - 07:25 PM
Here's the thing, when you exit a block of code, the variable declared in that block cease to exist. Functions CANNOT see each others variables. Parameters (especially pointers and references) are used to pass information back and forth between functions.
#5
Posted 17 February 2009 - 07:28 PM
yeah i remembered that pointers thing
it's like first we assume we know where that data is store, and assign something to that address (using pointer / references)
thanks
it's like first we assume we know where that data is store, and assign something to that address (using pointer / references)
thanks


Sign In
Create Account


Back to top









