473,769 Members | 3,305 Online
Bytes | Software Development & Data Engineering Community
+ 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 3854
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.home work.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
1513
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 variable might be used or unused in a given object. For each field, there is a FieldIsValid() member function that must be called before accessing or mutating a given field. If a field is reported as not valid, it must not be accessed or mutated....
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 initialized to zero. What does the standard define for this? thanx Andreas
5
2509
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 want all these variables to be initialized to a pattern of 0xFB. For example, for a 2 byte int, I want the value to be 0xFBFB, and for a 1 byte char, 0xFB, and 3 bytes, 0xFBFBFB. I suppose I could pack all the variables into a struct and simply...
0
1116
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 so that I don't have to hit the database every call. I would like to initialize these tables through calling a static class function, Init(), in application start. Thanks.
17
2623
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 main(String args) { MyType aMember; ... aMember = new MyType(...) ... } However, in C++ this does not seem to work. I declare in class (it's
6
3374
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
1906
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
2063
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 it by itself. I wanted to know if one does initialize fields in a class, how much of the performance hit would it have for that same operation without a initialization done.
8
7519
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, like this: for(var i=0; i<list; i++) { var name = list; }
10
1910
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 it's easy to solve: I just make my own default constructor. Problem: My own default constructor is considered to be *initializing the variable* (even though it doesn't), whereas the original one does not. Thus, when I declare and use it before...
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7413
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
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 we have to send another system
3
2815
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.