Re: Initialization is done before the program starts executing ?
- From: Jack Klein <jackklein@xxxxxxxxxxx>
- Date: Thu, 21 Sep 2006 23:11:17 -0500
On 21 Sep 2006 08:04:06 -0700, "lovecreatesbea...@xxxxxxxxx"
<lovecreatesbeauty@xxxxxxxxx> wrote in comp.lang.c:
K&R 2, sec 2.4 says: If the variable in question is not automatic, the
initialization is done once only, conceptually before the program
starts executing, ... .
"Non-automatic variables are initialized before the program starts
executing." -- What does this mean? What is the name of the stage in
which the mentioned initialization is performed? Compile-time or
run-time?
In the following snippet, variables b and c are defined at line 7 & 8
but the initialization or assignment to them is not performed. The
declaration/definition and initialization/assignment are in the same
scope, why the declaration is executed but the initialization is not?
#include <stdio.h>
int main(void){
int a = 1;
switch(a){
int b = 30; /*line 7*/
int c; /*line 8*/
c = 20; /*line 9*/
case 1: /*fall-down*/
default:
printf("b: %d, c: %d\n", b, c);
}
return 0;
}
/*Result:
b: 4598440, c: 4198571*/
Yesterday, in another thread about the same issue, I used the term
"flow of control".
I think you are slightly confused about two different concepts and
mixing them together.
These two concepts are scope and execution flow.
Scope is a compile time concept. A compiler processes a translation
unit in strict linear order (or at least must produce results as if it
did), from the first line of the source file through the last. A
simple description of the C standard "translation unit" is a source
file and anything it includes. Any headers or included files are
processed in linear order from their first line to their last, and
then processing returns to the next line in the file that included
them.
Every identifier in translation unit has a scope, from the point of
the identifier's declaration to the end of the scope it was declared
in. If it is declared at file scope in a translation unit, whether in
the source file or in something included, it is in scope from that
line through the last line in the source file. If an identifier is
defined inside any block, it has scope from the point of its
declaration until the end of that block.
When a C program is executed, it is actually very rare for the flow of
execution to be strictly in order from top to bottom. Flow control
statements like if, else, while, for, goto, and switch cause
statements to be skipped or looped. But this is a run time matter,
and has no effect on scope, which was a compile time matter.
So 'b' has a scope that starts on the line where it is declared (a
definition is also a declaration) and initialized. This scope ends
seven lines later on the line containing the closing brace of the
included switch statement.
C makes a distinction between initialization and an assignment. An
assignment is always a statement, a declaration with an initializer is
not a statement. But for an automatic variable, an initializer is
"like" a statement, in that it only actually happens if the flow of
control passes through it.
It can be useful to imagine something that you could call "an
execution pointer", that points to each statement as it is executed.
If the "execution pointer" never points to the declarator that
initializes an auto object, that initialization is not performed.
But this has no effect on the scope of the object's identifier, which
has nothing at all to do with when, or if, the "execution pointer"
ever touches the identifier's declaration.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
.
- Follow-Ups:
- Re: Initialization is done before the program starts executing ?
- From: lovecreatesbea...@xxxxxxxxx
- Re: Initialization is done before the program starts executing ?
- References:
- Initialization is done before the program starts executing ?
- From: lovecreatesbea...@xxxxxxxxx
- Initialization is done before the program starts executing ?
- Prev by Date: Re: Why pointers??
- Next by Date: Re: qustion about function pointer.
- Previous by thread: Re: Initialization is done before the program starts executing ?
- Next by thread: Re: Initialization is done before the program starts executing ?
- Index(es):
Relevant Pages
|