Jump to content

Initialise/initialize Variables At Start


Recommended Posts

Hi according to previous C I've used, char variables at declaration were default value 0.

It seems that this is not so with BoostC?

If not, whats the preferred method to initialise a bunch of variables at same time.

eg must one do v1=0;v2=0;v3=0;

not v1=v2=v3=0;

Cheers,

Al

PS understood that assigning variables at start can use memory, but only if some of the variables are not being used correct?

Link to post
Share on other sites

If you declare a variable at 'static' it will be initialised to zero unless you tell the compiler otherwise (BoostC manual page 22).

 

If you wish to zero a bunch of variables to zero use the v1=0, v2=0 etc method. The other method does not zero all your variables, only the last one.

 

Cheers

 

Reynard

Link to post
Share on other sites
Isn't that what I said Ian ?

 

:-)

 

Okay to be clear:

 

if you don't explicitly initialise a variable, eg,

 

 

int my_variable;

 

it *will* be initialised to 0 unless you use the command line switch.

 

In a lot of cases it really doesn't matter what the initial value is, you can save memory by not forcing everything to zero.

 

cheers

Ian.

Link to post
Share on other sites
Isn't that what I said Ian ?

 

:-)

 

Okay to be clear:

 

if you don't explicitly initialise a variable, eg,

 

 

int my_variable;

 

it *will* be initialised to 0 unless you use the command line switch.

 

In a lot of cases it really doesn't matter what the initial value is, you can save memory by not forcing everything to zero.

 

cheers

Ian.

Actually, I could stand to be corrected on this, but the reason I was initializing was due to some startup numbers not being at zero. Could always be me of course!

Cheers,

Al.

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...