Alfonso Morra wrote:
Ok, this is a bit of a quick hack. I have a flag set in another file
(the default value of the flag is 0. In certain files, access to the
flag symbol is not available - I want to know if unresolved symbols are
given the value of 0 ?. Is this compiler specific or part of the C standard?
PS: It is too long winded to explain WHY I need to do this - I just need
to know if it will work reliably - i.e. I can count on an unresolved
external symbol(assumed to be an int) will have the value of 0.
Let me rephrase your question, to be sure we both
mean the same thing by "unresolved symbol." In file a.c
you have
extern int flag;
Usually, your program is built from both a.c and another
file b.c which contains (at file scope, that is, not inside
any function)
int flag = 0; /* or perhaps some other value */
For some reason you decide to build the program from a.c
alone, without b.c to provide the definition of `flag'.
During the build process, you get a message about `flag'
being "unresolved."
If that's your situation, then no: You cannot count on
any particular value for `flag'. You cannot even be sure
that `flag' exists ("unresolved" means that the linker was
not able to find a `flag' anywhere, so its non-existence is
more probable than not). You cannot even be sure that the
build process will produce an executable program; many linkers
will simply fail if unable to satisfy all the references the
program says it requires, some will produce a program-like
file that is somehow marked "not executable" until and unless
you take special action.
From the standpoint of the C language, there are two cases.
If the program ever refers to `flag' in an expression (other
than as the operand of sizeof), there must be exactly one
definition of `flag' in some translation unit ("source file")
that is part of the program. If the program never refers to
`flag' in any expression, it is permissible for `flag' not to
be defined at all. Anything else is simply not a correct C
program.
Since you're concerned about the value of `flag', you
clearly intend to use `flag' in an expression somewhere (that's
the only way you can inspect or change its value). Therefore
the first case applies: Somewhere, `flag' must be defined and
defined exactly once, or else you don't have a program.
--
Er*********@sun.com