initializing variables

F

Felix Kater

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
 
A

Artie Gold

Felix said:
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 not 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
 
L

Lawrence Kirby

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,607
Members
45,241
Latest member
Lisa1997

Latest Threads

Top