Hi my friend here is a better answer for you:
What is the scope for this varaible(MyVariable)?
for(int i=0;i<10;i++)
{
int MyVariable; // the scope of this variable is local because
do something; // you only have acces to the variable inside of
} // the for statement;
On the othet hand we have global variables:
#include<stream.h>
int MyGlobalVariable = 5; //This is a global variable because is
//declared before the main function
int main() // this meand that you can use this
{ // variable on any part of the code
// including in "for" statements.
return 0;
}
Ok! so the scope of variable depend where you declare the variable (local or global).
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.
|