473,225 Members | 1,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

Initialization is done before the program starts executing ?

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*/

Sep 21 '06 #1
8 3075
On 2006-09-21, lovecreatesbea...@gmail.com <lo***************@gmail.comwrote:
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?
It means that each non-automatic variable is set to its initial value
before the function of the C program that is called at program
starup is called. The name of the function called at program startup
is usually "main". The initialization can be preformed any time
before the first statement of function called at program startup
executes. Different compilers and execution environments may do the
initiailzation at different times.
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?
Because the variables declared/defined at lines 7 & 8 are automatic
variables, the above quote from K&R2 does not apply to them. For
more information, please see my reply (message-id
<sl*********************@earl-grey.cloud9.net>) in the "why does C
standard allow this declaration" thread of this newsgroup.
#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*/
Sep 21 '06 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
lovecreatesbea...@gmail.com wrote:
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?
It means that the programmer can presume that s/he does not have to
write explicit program logic to initialize non-automatic variables
What is the name of the stage in which the mentioned initialization is
performed? Compile-time or run-time?
Yes. No. Either or, or both.

For the purposes of K&R, (and likely of the C standard as well), it
doesn't matter how the compiler's architect chose to implement such
initialization. It only matters that such initialization takes place.

The architect could arrange for the initialization of non-automatic
variables to occur as part of a start-up code that executes
(conceptually) before main() is invoked.

Or, the architect could arrange for the compile and link process to
write out a loadable "image" of the memory assigned to non-automatic
variables, complete with all the blanks filled in, so that when the
system loads the program, the initialization has already occurred.

Or, the architect could arrange for a combination of the two, or even
some other mechanism to occur. In any case, from the programmers point
of view, such initialization takes place outside of and before his
logic, and is transparent to him.
In the following snippet, variables b and c are defined at line 7 & 8
but the initialization or assignment to them is not performed.
For one thing, they are not "non-automatic variables".

Think "static" or "extern" rather than "auto" or default scope.
The
declaration/definition and initialization/assignment are in the same
scope, why the declaration is executed but the initialization is not?
P'haps one of our resident experts can answer this better. Think of the
statement
int b = 30;
as doing three separate things.

It
1) tells the compiler that, when the programmer references the variable
"b", treat such references as accesses to an integer value stored as an
automatic variable,
2) generates the code to allocate the space for that automatic
variable, and
3) generates the code to initialize the automatic variable.

It is likely that any optimization that the compiler does for automatic
variables "rolls up" all variables (within a particular level of scope,
in this case bounded by the edges of the switch() compound statement)
into one big allocation, which happens at the entry to the scope.

However, it is less likely that the compiler can perform the same sort
of "roll up" operation on initializations, and it might leave them as
hidden operations to be performed in sequence after the entry to the
scope level. Now, since your switch() function branches to specific
entrypoints in the compound statement, these initializations are
possibly bypassed.
#include <stdio.h>

int main(void){
int a = 1;
a is an automatic variable
switch(a){
int b = 30; /*line 7*/
b is an automatic variable
int c; /*line 8*/
c is an automatic variable
c = 20; /*line 9*/

case 1: /*fall-down*/
default:
printf("b: %d, c: %d\n", b, c);
}

return 0;
}

/*Result:
b: 4598440, c: 4198571*/
HTH
- --
Lew Pitcher
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12

iD8DBQFFErNaagVFX4UWr64RAgVtAKCm8ry42nli/k4ISnFT3qY7Hbk3cACfdu32
kUeNYIkk1LnXXnGQT1NzZUs=
=y5JV
-----END PGP SIGNATURE-----

Sep 21 '06 #3

A. Bolmarcich wrote:
On 2006-09-21, lovecreatesbea...@gmail.com <lo***************@gmail.comwrote:
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?

It means that each non-automatic variable is set to its initial value
before the function of the C program that is called at program
starup is called. The name of the function called at program startup
is usually "main". The initialization can be preformed any time
before the first statement of function called at program startup
executes. Different compilers and execution environments may do the
initiailzation at different times.
If the main is not called and the program never even starts, how can
the non-automatic variables are defined and initialized?
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?

Because the variables declared/defined at lines 7 & 8 are automatic
variables, the above quote from K&R2 does not apply to them. For
more information, please see my reply (message-id
<sl*********************@earl-grey.cloud9.net>) in the "why does C
standard allow this declaration" thread of this newsgroup.
#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*/
You mean the initialization and assignment at line 7 & 9 are
unreachable, but why the declaration and definition at the same line 7
and line 8 are reached?

Sep 21 '06 #4

Lew Pitcher wrote:
lovecreatesbea...@gmail.com wrote:
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?

It means that the programmer can presume that s/he does not have to
write explicit program logic to initialize non-automatic variables
What is the name of the stage in which the mentioned initialization is
performed? Compile-time or run-time?

Yes. No. Either or, or both.

For the purposes of K&R, (and likely of the C standard as well), it
doesn't matter how the compiler's architect chose to implement such
initialization. It only matters that such initialization takes place.

The architect could arrange for the initialization of non-automatic
variables to occur as part of a start-up code that executes
(conceptually) before main() is invoked.

Or, the architect could arrange for the compile and link process to
write out a loadable "image" of the memory assigned to non-automatic
variables, complete with all the blanks filled in, so that when the
system loads the program, the initialization has already occurred.

Or, the architect could arrange for a combination of the two, or even
some other mechanism to occur. In any case, from the programmers point
of view, such initialization takes place outside of and before his
logic, and is transparent to him.
Thank you. Your explanation is helpful to me.
In the following snippet, variables b and c are defined at line 7 & 8
but the initialization or assignment to them is not performed.

For one thing, they are not "non-automatic variables".

Think "static" or "extern" rather than "auto" or default scope.
Yes, I just asked another question :)
The
declaration/definition and initialization/assignment are in the same
scope, why the declaration is executed but the initialization is not?

P'haps one of our resident experts can answer this better. Think of the
statement
int b = 30;
as doing three separate things.

It
1) tells the compiler that, when the programmer references the variable
"b", treat such references as accesses to an integer value stored as an
automatic variable,
2) generates the code to allocate the space for that automatic
variable, and
3) generates the code to initialize the automatic variable.

It is likely that any optimization that the compiler does for automatic
variables "rolls up" all variables (within a particular level of scope,
in this case bounded by the edges of the switch() compound statement)
into one big allocation, which happens at the entry to the scope.

However, it is less likely that the compiler can perform the same sort
of "roll up" operation on initializations, and it might leave them as
hidden operations to be performed in sequence after the entry to the
scope level. Now, since your switch() function branches to specific
entrypoints in the compound statement, these initializations are
possibly bypassed.
#include <stdio.h>

int main(void){
int a = 1;

a is an automatic variable
switch(a){
int b = 30; /*line 7*/

b is an automatic variable
int c; /*line 8*/

c is an automatic variable
c = 20; /*line 9*/

case 1: /*fall-down*/
default:
printf("b: %d, c: %d\n", b, c);
}

return 0;
}

/*Result:
b: 4598440, c: 4198571*/
Sep 21 '06 #5
On 2006-09-21, lovecreatesbea...@gmail.com <lo***************@gmail.comwrote:
>
A. Bolmarcich wrote:
>It means that each non-automatic variable is set to its initial value
before the function of the C program that is called at program
starup is called. The name of the function called at program startup
is usually "main". The initialization can be preformed any time
before the first statement of function called at program startup
executes. Different compilers and execution environments may do the
initiailzation at different times.

If the main is not called and the program never even starts, how can
the non-automatic variables are defined and initialized?
If the program never even starts, it does not matter whether
non-automated been initialized.
>Because the variables declared/defined at lines 7 & 8 are automatic
variables, the above quote from K&R2 does not apply to them. For
more information, please see my reply (message-id
<sl*********************@earl-grey.cloud9.net>) in the "why does C
standard allow this declaration" thread of this newsgroup.
#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*/

You mean the initialization and assignment at line 7 & 9 are
unreachable, but why the declaration and definition at the same line 7
and line 8 are reached?
Declarations are not executable statements; the initialzation of an
automatic variable is like an executable assignment expression. Please
read the reply in the other thread that I mentioned.
Sep 21 '06 #6
On 21 Sep 2006 08:04:06 -0700, "lovecreatesbea...@gmail.com"
<lo***************@gmail.comwrote 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/~a...FAQ-acllc.html
Sep 22 '06 #7

Jack Klein wrote:
On 21 Sep 2006 08:04:06 -0700, "lovecreatesbea...@gmail.com"
<lo***************@gmail.comwrote 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*/
<snip>
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.
Thank you.

Why can the initialization/assignment at line 7 & 9 above be omitted
but their declaration can't? When is a variable declared/defined? It
should not be declared when you write down that line of code in the
source editor, or when the compiler is invoked.

Sep 22 '06 #8

lovecreatesbea...@gmail.com wrote:
#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*/
Why can the initialization/assignment at line 7 & 9 above be omitted
but their declaration can't? When is a variable declared/defined? It
should not be declared when you write down that line of code in the
source editor, or when the compiler is invoked.
Why not? What source do you have to validate that assertion?

I think you are in danger of defining your own C standard and
complaining when the real C standard doesn't match your expectations.
Both Lew and Jack have given detailed explanations, and Keith
Thompson's discussion in another thread you started on the same issue
gives a very clear explanation.

Accept that that's the way the language works and move on.

Sep 22 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vinay Aggarwal | last post by:
I have been thinking about the lazy initialization and double checked locking problem. This problem is explain in detail here http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html...
13
by: Nolan Martin | last post by:
How would I go about calling a function before the main function is called? The function needs to be called multiple times from different locations. I have something like this working but it is...
12
by: Ricardo Pereira | last post by:
Hello all, I have a C# class (in this example, called A) that, in its constructor, starts a thread with a method of its own. That thread will be used to continuously check for one of its...
6
by: Neelesh Bodas | last post by:
Hello All, I was just listing down various ways in which variables can be created and destroyed in C++. (On the lines of 10.4.3 TC++PL Ed 3) Putting the summary here for corrections, comments,...
4
by: mnowosad | last post by:
As far I know, static variables are tied to AppDomain scopes. So, every time an executing code within an AppDomain references a class for the the first time since the AppDomain was created/loaded,...
7
by: Spoon | last post by:
Hello everyone, I have a Packet class I use to send packets over the Internet. All the packets sent in a session are supposed to share a common random ID. I figured I'd use a static const...
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
20
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.)...
4
by: riva | last post by:
Why the static variable in a function should be initialized by a constant? Why not any normal variable.
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.