Static global variables
--- accessible within
all functions
defined inside the
same C program file
Static local variables ---
accessible within the
one C function
Common property:
The life time of these kinds of
variables is the
whole execution period of
the C program
Short term variables:
(a.k.a.: run-time variables)
Local variables
Parameter variables
Common property:
The memory space used for
these kinds of variables
are allocated (reserved)during the
execution (running) of the program
They are created on the
system stack
(just like what you learned in
assembler programming !!!)
How to define (recognize)
the different kinds of variables
Just like Java, the
location (place) where the
variable is defined
in the (C) program determines
the kind of variable
that will be defined:
Parameter variables
are defined inside the
braces ( ... )
of the function header
Example:
int main( int argc, char *argv[] )
{ ^^^^^^^^^^^^^^^^^^^^^^^^
parameter variables
...
...
}
(Just like Java)
Local variables
are defined inside the
body of a function
(i.e., between the
braces { ... }
of the function body)
Example:
int main( int argc, char *argv[] )
{
int a; double b;
^^^^^^^^^^^^
local variables
...
if ( .... )
{
int x;
^^^^^^^^^^
local variable
}
...
}
(Just like Java)
Global variables
are defined outside a
functionwithout using
the keywordstatic:
int a; // Global variable int b; // Global variable
int main ( int argc, char *argv[] )
{
...
...
}
double c; // Global variable
void f( .... )
{
...
...
}
(The 2 different kinds of
global variables
are distinguished
by the
static keyword.)
Static global variables
are defined outside a
functionusing
the keywordstatic:
static int a; // Static global variable static int b; // Static global variable
int main ( int argc, char *argv[] )
{
...
...
}
double c; // Global variable
void f( .... )
{
...
...
}
(The 2 different kinds of
global variables
are distinguished
by the
static keyword.)
Static (local) variables
are defined inside a
function using the
keyword static:
int main ( int argc, char *argv[] )
{
static double d; // Static variable
double f; // Normal local variable !
...
...
}
double c; // Global variable
void f( .... )
{
...
...
}