Mail Archives: djgpp/1997/10/05/21:16:34
Vic wrote:
>
> Liche wrote:
> >
> > Liche wrote:
> >
> > I am sorry, it displays 21 ten times if I use auto short count = 20;,
> > instead of 21 22 23...
> > Anyways, this is still a weird result. It seems that if I don't
> > initialize a variable to a value then it is static, even if i use auto
> > short count; ......
> > Can anybody else reproduce this?
> yes, me. And indeed I find it weird. Cause if I say something like
> auto int x; then I print the value of X it will be something random.
it is absolutely not weird. 'auto' is not a synonym for 'random variable
generator'. the whole point is the value of a non-static local variable
is not initialized, it is undefined. undefined does not mean every time
the variable is created, the compiler is going to spend extra effort
making sure that the value it points to contains some 'random' (whatever
you guys are using it to mean) value. the variable is not initialized.
the value it holds is undefined. therefore, it can be anything including
your birthday.
--
----------------------------------------------------------------------
A. Sinan Unur
Department of Policy Analysis and Management, College of Human Ecology,
Cornell University, Ithaca, NY 14853, USA
mailto:sinan DOT unur AT cornell DOT edu
http://www.people.cornell.edu/pages/asu1/
- Raw text -