468,784 Members | 1,596 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,784 developers. It's quick & easy.

initializing variables

Some basic questions about variable initialization:

a. Is it legal to initialize the variables i, b and l like this (see below) ?

b. If so: At what time are they initialized? In other words: how can I
imagine where the compiler puts the code to initialize the variables? Is it done somehow this way that the compiler puts the values of the variables once into memory right as it does with the commands?

c. Are boolean variables exceptions that are guaranteed to default to
false even when not initialized?
int i = 99;
bool b = true;
void f(){
static long l = 9999;
}
int main(){
f();
}
Felix
Nov 14 '05 #1
3 3533
Felix Kater wrote:
Some basic questions about variable initialization:

a. Is it legal to initialize the variables i, b and l like this (see below) ?

b. If so: At what time are they initialized? In other words: how can I
imagine where the compiler puts the code to initialize the variables? Is it done somehow this way that the compiler puts the values of the variables once into memory right as it does with the commands?

c. Are boolean variables exceptions that are guaranteed to default to
false even when not initialized?
int i = 99;
bool b = true;
void f(){
static long l = 9999;
}
int main(){
f();
}
Felix


Please provide your instructor's email address, yada yada yada....

IOW - this is news:comp.lang.c, not news:do.my.homework.for.me; get a
good book about C (see http://www.accu.org for reviews and suggestions)
and do your own work.

When you have a question about C -- that's not obviously a homework
problem -- by all means come back and ask it, after, of course, reading
the fine FAQ and searching the archives.

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #2
Hi,

I'll ask in a compiler's newsgroup again:
how can I
imagine where the compiler puts the code to initialize the [global] variables?


Felix
Nov 14 '05 #3
On Tue, 10 May 2005 00:06:25 +0200, Felix Kater wrote:
Some basic questions about variable initialization:

a. Is it legal to initialize the variables i, b and l like this (see below) ?
Yes.
b. If so: At what time are they initialized? In other words: how can I
imagine where the compiler puts the code to initialize the variables? Is it done somehow this way that the compiler puts the values of the variables once into memory right as it does with the commands?
A variable is initialised when it is created. Variables with static
storage duration (which includes all 3 variables here) are created
exactly once during the execution of the program, before main() is called.
c. Are boolean variables exceptions that are guaranteed to default to
false even when not initialized?


Boolean variables (in C99) are not exceptions. A variable with automatic
storage duration without an initialiser is not initialised; its value
should not be accessed before it is set. A variable with static storage
duration without an explicit initialiser is initialised as if 0 is
assigned to it. This applies equally to booleans, noting that assigning 0
to a boolean will result in its value being set to false.

Lawrence
Nov 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Dave | last post: by
1 post views Thread by Andreas Boehm | last post: by
5 posts views Thread by jyu.james | last post: by
reply views Thread by Adam Smith | last post: by
17 posts views Thread by Calle Pettersson | last post: by
6 posts views Thread by alacrite | last post: by
2 posts views Thread by eriwik | last post: by
14 posts views Thread by Sugandh Jain | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.