473,487 Members | 2,461 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3826
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1496
by: Dave | last post by:
Hello all, I have a class that contains a large number of discrete pieces of state information. Any combination of these member variables might be valid for a given object. Any given member...
1
417
by: Andreas Boehm | last post by:
Hi *.*, does the standard meanwhile define something about initializing variables by the compiler? I think, it is a side-effect of the OS used, if undefined global (static) variables are...
5
2490
by: jyu.james | last post by:
Is there an easy way to specify that global variables be initialized to something else besides zero? Specifically, I have a project that has many global variables spread across many files. I...
0
1102
by: Adam Smith | last post by:
Are there any drawbacks to initializing static member variables for classes used in an asp.net application within application_start? My classes have arraylist like tables, which cache information...
17
2576
by: Calle Pettersson | last post by:
Coming from writing mostly in Java, I have trouble understanding how to declare a member without initializing it, and do that later... In Java, I would write something like public static void...
6
3349
by: alacrite | last post by:
If I have this situation class X { Z z; Y y; }; Class X has two objects of type Z and Y. How do I initialize z and y with non default constructors?
2
1889
by: eriwik | last post by:
Given a simple class like class test { private: size_t size_; int* data_; public: test(size_t s) : size_(s), data_(new int { /* ... */ };
14
2024
by: Sugandh Jain | last post by:
Hi, The warning from Microsoft.Performance Code Analysis check that, its not required to initialize numeric variables to zero, boolean to false and object to null is a good one because CLR does...
8
7495
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
10
1873
by: Jason Doucette | last post by:
Situation: I have a simple struct that, say, holds a color (R, G, and B). I created my own constructors to ease its creation. As a result, I lose the default constructor. I dislike this, but...
0
7108
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7142
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6847
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7352
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5445
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4875
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
272
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.