REAL BASIC HELP

The declaration below will assign zero to the integer variable x.
int x;
Is this true or false?
I'm new to c++
Last edited on
Look over your class notes.

Hint: is 'x' a global or a local variable?
Hello LyonPredator,

False.

Check the difference between declaration and definition.

Hope that helps,

Andy
Hey Handy Andy, don't be too sure of yourself there.

Hint: is 'x' a global or a local variable?
closed account (NCRLwA7f)
I'm no expert so please anyone correct me if I'm wrong, but the idea is

if you don't assign anything to a variable then there is a good chance that it will have some random value until you either assign a value to it or you initialize it.

example:

int x; // this x value can be 48484503023 or -234932493249 or some other crazy value by default

but if you do this:

int x = 0; // then x will have the value zero

now when you start using x such as

int x = 0;
x = 35 + 5; // x value is 40 now.

hope this helps

Last edited on
If you follow the hint, you might get into http://en.cppreference.com/w/cpp/language/default_initialization

It is thus crystal clear (?) that the answer to
Is this true or false?

is: Yes

Yes, it is true.
Yes, it is false.

A followup question is: Has the class touched all the cases? I'd guess 'no'.


What is infuriating, is that some compilers in some modes do initialization that the standard does not require. That leads to unwarranted "But it works!" claims.
@Duthomhas,

Sorry I did not realize that global or local made a difference. Always nice to learn something new.

I have had the understanding that all variables should be initialized and that is what I was working from.

Thank you,

Andy
@Handy Andy
The “initialize everything” rule of thumb is a very good one to follow. It will save you a lot of grief. Some company code guidelines explicitly require it.
Topic archived. No new replies allowed.