C++ Builder: Scope of VariablesA scope is a region of the program. There are two places where variables can be declared: Local variablesVariables that are declared inside a function or code block are local variables. #include... int SomeFunction() { int G = 0; // initialize a local variable before using it ... }
Global variablesGlobal variables are defined outside of all the functions, usually near the top of a unit. Global variables are initialized automatically by the system when you define them as follows: Data type: initialized to: int 0 char '\0' float 0 double 0 pointer NULL Example: #include... // Global variable declaration: int g; // g is automatically initialized to 0 A local variable can have the same name as a global variable, but inside a function the local variable has priority. Example: #include... // Global variable declaration: int g = 20; int SomeFunction() { // Local variable declaration: int g = 10; int a; a = g; // a will become 10 return a; } |