The compiler is stupid, basically. It will not realize that all possibilities ensure that y will be initialized. However, if you replace the second if with an else, the program will compile and run. Just to be on the safe side, make it a habit of initializing all local variables at the onset.
the compiler is ensuring that y is initialized. Since the value of x could change, there is no guarantee(to the compiler) that y is initialized. If you initialize y = 0, this will compile and run. Line 1 in main() int x = 10, y = 0;
Joined: Apr 23, 2002
Thanks, that sounds trip cuestion. I know, is the best way to make it a habit of initializing all local variables at the onset. But the exam have trip/traps Thank you very much