Alex Rivera | Logout

Why do I see strange values when I print uninitialized variables?

Asked 2010-11-23T19:20:26.633
13

In the following code, the variable has no initial value and printed this variable.

int var;
cout << var << endl;

output : 2514932

double var;
cout << var << endl;

output : 1.23769e-307

I don't understand these output numbers. Can any one explain this to me?

Edit
Report

1 Answer

4

When you do:

int var;

You are only declaring an integer named var. You do not initialize it with a value, so in whatever location var is, will be garbage data.

int var = 5;

Would declare var and initialize it to 5.

See more: http://en.wikipedia.org/wiki/Uninitialized_variable

answered 2010-11-23T19:23:15.960

Your Answer