johnnash said:
What is a static object ?
It is an object that persists throughout the lifetime of the program and
retains it's last stored value. There are different ways to declare a
static object.
When you declare a variable outside of any function it becomes a file
scope static object. It becomes accessible to all the following
portions of that source file and, with proper extern declarations, in
other modules as well.
When you declare a variable within a function or a block like this:
static TYPE NAME;
it defines a static object that is visible only to that function or
block, but retains it's last stored value even after that function or
block is left by the flow of execution and perhaps reentered later.
but even in that program, a is not defined
until call function where a is defined and assigned the value of 3, am
i right ?
'a' is defined in a.c. The declaration in a.h is meant for inclusion in
other files so that those files know about 'a' and can access it. 'a'
is defined in line 2 of a.c. It is assigned the value 3 in line 4.
So how can you print value of variable if its not even
defined ?
It is defined in a.c whose object code you include in the final
executable. It is not defined in main.c, but the declaration of 'a' in
a.h, which is included in main.c on line 3 ensures that the compiler
can successfully translate main.c and emit the correct object code for
the two printf statements.
The whole purpose of extern is to announce the properties of objects
which are not defined in the current scope.
Maybe you should read a C tutorial. A good one is:
<
http://www.eskimo.com/~scs/cclass/cclass.html>