whats the difference between local and global variables?(with illustrations)
Four answers:
2008-10-04 11:32:12 UTC
LOCAL AND GLOBAL VARIABLES
Local
These variables only exist inside the specific function that creates them. They are unknown to other functions and to the main program. As such, they are normally implemented using a stack. Local variables cease to exist once the function that created them is completed. They are recreated each time a function is executed or called.
Global
These variables can be accessed (ie known) by any function comprising the program. They are implemented by associating memory locations with variable names. They do not get recreated if the function is recalled.
DEFINING GLOBAL VARIABLES
/* Demonstrating Global variables */
#include
int add_numbers( void ); /* ANSI function prototype */
/* These are global variables and can be accessed by functions from this point on */
int value1, value2, value3;
int add_numbers( void )
{
auto int result;
result = value1 + value2 + value3;
return result;
}
main()
{
auto int result;
value1 = 10;
value2 = 20;
value3 = 30;
result = add_numbers();
printf("The sum of %d + %d + %d is %d\n",
value1, value2, value3, final_result);
}
Sample Program Output
The sum of 10 + 20 + 30 is 60
The scope of global variables can be restricted by carefully placing the declaration. They are visible from the declaration until the end of the current source file.
#include
void no_access( void ); /* ANSI function prototype */
void all_access( void );
static int n2; /* n2 is known from this point onwards */
void no_access( void )
{
n1 = 10; /* illegal, n1 not yet known */
n2 = 5; /* valid */
}
static int n1; /* n1 is known from this point onwards */
void all_access( void )
{
n1 = 10; /* valid */
n2 = 3; /* valid */
}
AUTOMATIC AND STATIC VARIABLES
C programs have a number of segments (or areas) where data is located. These segments are typically,
_DATA Static data
_BSS Uninitialized static data, zeroed out before call to main()
_STACK Automatic data, resides on stack frame, thus local to functions
_CONST Constant data, using the ANSI C keyword const
The use of the appropriate keyword allows correct placement of the variable onto the desired data segment.
/* example program illustrates difference between static and automatic variables */
#include
void demo( void ); /* ANSI function prototypes */
void demo( void )
{
auto int avar = 0;
static int svar = 0;
printf("auto = %d, static = %d\n", avar, svar);
++avar;
++svar;
}
main()
{
int i;
while( i < 3 ) {
demo();
i++;
}
}
Sample Program Output
auto = 0, static = 0
auto = 0, static = 1
auto = 0, static = 2
Static variables are created and initialized once, on the first call to the function. Subsequent calls to the function do not recreate or re-initialize the static variable. When the function terminates, the variable still exists on the _DATA segment, but cannot be accessed by outside functions.
Automatic variables are the opposite. They are created and re-initialized on each entry to the function. They disappear (are de-allocated) when the function terminates. They are created on the _STACK segment.
Please give me 5 stars if My answer help U so much! THX! ^_^
MountainDew
2008-10-04 07:53:36 UTC
Global means you can use it in any event, local means you can only use it in the event where it's declared.
Example, C++:
#include
using namespace std;
bool x; //Global Boolean variable, can be used anywhere
void test()
{
//x can be used here, but y can't
}
int main();
{
int y; //Local y integer, can only be used in this event
{
VB6:
Dim x as string 'global x variable, since it's outside the events
Private Sub Command1_Click()
Dim y as integer 'local y variable, since it's inside an event
End Sub
hasir
2016-12-01 12:10:36 UTC
interior of sight ,international variables are specifically in touch with the interior music additionally there are Public, inner maximum specifically 2 variety of Variables style beside buddy etc.. the variable scoop evaluate the place it rather is declared in case you finished in factor of a journey or a function of Scripts (VB JavaScript etc..) then it rather is been seen as interior of sight Variable via fact it rather is purely constrained interior that function or journey in Programming C#, Java ,C++ international Variables -------------------------- type className { int _variable_1 ; string _variable_2 ; double _variable_3 ; char _variable_4 ; bool _variable_5 ; the interior of sight variables ------------------------------ void methodName() { int _variable_1 ; string _variable_2 ; double _variable_3 ; char _variable_4 ; bool _variable_5 ; } } in Scripting -->