Topic: Uninitialized variables
Author: meli@js-sfbsun.cs.uni-sb.de (Meli Henry)
Date: 21 Oct 1994 03:27:52 GMT Raw View
Srinivasulu Vuggumudi (srini@gab.unt.edu) wrote:
: I am a C++ beginner. In C++ if a variable is not initialized by the
: programmer it will be initialized by the compiler to zero. This is what I
: read in a book. I wrote a small code totest this. I used Borland C++ ver 3.1
: compiler.
: #include <iostream.h>
: void main(void) {
: int x ;
: float y ;
: cout << "This is the value of X:" << x << endl ;
: cout << "This is the value of Y:" << y << endl ;
: }
Variables with storage class static that are not initialized are guaranteed
to start off as 0 converted to the specify type. The initial values of automatic and register variables that are not initialized are undefined.
Bye, Henri.
Author: srini@gab.unt.edu (Srinivasulu Vuggumudi )
Date: Thu, 13 Oct 1994 16:53:41 GMT Raw View
I am a C++ beginner. In C++ if a variable is not initialized by the
programmer it will be initialized by the compiler to zero. This is what I
read in a book. I wrote a small code totest this. I used Borland C++ ver 3.1
compiler.
#include <iostream.h>
void main(void) {
int x ;
float y ;
cout << "This is the value of X:" << x << endl ;
cout << "This is the value of Y:" << y << endl ;
}
After executing the above program ; I am getting a four figure value for
x and 0 for y. I think I am getting both x and y wrong. x and y values should
be displayed as 0 and 0.00.
Suggestions Please .......
........... Srini
Author: b91926@fsgm01.fnal.gov (David Sachs)
Date: 13 Oct 1994 12:36:20 -0500 Raw View
srini@gab.unt.edu (Srinivasulu Vuggumudi ) writes:
>I am a C++ beginner. In C++ if a variable is not initialized by the
>programmer it will be initialized by the compiler to zero. This is what I
>read in a book. I wrote a small code totest this. I used Borland C++ ver 3.1
>compiler.
>#include <iostream.h>
>void main(void) {
>int x ;
>float y ;
>cout << "This is the value of X:" << x << endl ;
>cout << "This is the value of Y:" << y << endl ;
>}
>After executing the above program ; I am getting a four figure value for
>x and 0 for y. I think I am getting both x and y wrong. x and y values should
>be displayed as 0 and 0.00.
The values of local variables are undefined until they are
initialized or assigned to. Depending on whatever is left in
memory from previous operations, x and y could have any values
whatsoever.
Author: jason@cygnus.com (Jason Merrill)
Date: Thu, 13 Oct 1994 19:16:44 GMT Raw View
>>>>> Srinivasulu Vuggumudi <srini@gab.unt.edu> writes:
> I am a C++ beginner. In C++ if a variable is not initialized by the
> programmer it will be initialized by the compiler to zero. This is what I
> read in a book.
This is only true of variables of static storage duration, i.e. variables
declared at file scope or declared at function scope with the 'static'
keyword.
Jason
Author: cflatter@nrao.edu (Chris Flatters)
Date: 13 Oct 1994 20:16:29 GMT Raw View
In article 782067221@gab.unt.edu, srini@gab.unt.edu (Srinivasulu Vuggumudi ) writes:
>I am a C++ beginner. In C++ if a variable is not initialized by the
>programmer it will be initialized by the compiler to zero. This is what I
>read in a book. I wrote a small code totest this. I used Borland C++ ver 3.1
>compiler.
Only variables with storage class static (that is: only global
variables and local variables declared with the static keyword) are
guaranteed to be initialized if no initializer is given with their
declaration. In the absence of an initializer, the initial values of
automatic and register variables are undefined.
---
------------------------------------------------------------------------------
Chris Flatters cflatter@nrao.edu
------------------------------------------------------------------------------
Well, our problem stems from the fact that we, basically, allow every planet
and moon-base this side of Alpha Centauri to make their own version of Spam.
"Mystery Meat"
Man... or Astroman?