Re: Initialization is done before the program starts executing ?



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
.



Relevant Pages

  • Re: Question on switch block scope
    ... >> initialized for the given initialization value. ... > but we know that x is in scope and accessible from the point of its ... x's lifetime extends from the start of its enclosing block to the end. ... the scope extends from the declaration to the end of the ...
    (comp.lang.c)
  • Re: Question on switch block scope
    ... > initialized for the given initialization value. ... but we know that x is in scope and accessible from the point of its ... declaration to the end of the block in which it is declared. ... For each different entity that an identifier designates, ...
    (comp.lang.c)
  • Re: Initialization is done before the program starts executing ?
    ... initialization is done once only, ... execution pointer", that points to each statement as it is executed. ... ever touches the identifier's declaration. ...
    (comp.lang.c)
  • Re: C99: Suggestions for style(9)
    ... I do NOT want to see variable declaration "not at the top of a block". ... Neither are all declarations grouped together nor are declarations nicely connected to their initialisation and their scope is minimal. ... -then in alphabetical order; multiple ones per line are okay. ... Conditional initialization must of course come after the condition, and can not be in the same line as the declaration anyhow. ...
    (freebsd-hackers)
  • Re: Question on switch block scope
    ... >>The assignment of 123 to x never happens ... > Is this not a declaration with initialization, ... the declaration is reached in the order of execution, ...
    (comp.lang.c)