With respect to the question of why, on "various computers", some
code with undefined behaviour always happened to produce the same result:
The answer, Vaibhav87, is that you did not happen to use a variety
of computers or compilers. All the machines you happened to try
were the same as far as the compilers were concerned.
On SGI IRIX with SGI's MipsPro C compiler, the code happens to
produce 0.
On the same SGI IRIX machine with gcc 3.3, the code
happens to produce 2147430164 which is more easily recognized in
its hex format, 0x7fff2f14 . That happens to be a typical stack pointer
address in this machine.
So what has happened is that on stack-based compilers, the compiler
reaches in to the stack to the offset that it expects the variable to
be at, and uses whatever value it happens to find there, but it
does not specifically initialize the value before it starts because
it hasn't been told to do that.
On the machines you've been trying, the value that happens to be lying
on the stack has been 842.
I will make a prediction: whatever machine you are trying it on has
16 bit int. 842 decimal is 0x034a hex, which could easily be
the bottom 16 bits of a 32 bit pointer that just happened to be in
memory there as a side-effect of the program start-up. Or it could
have originated with something else completely. If you *really*
want to know why your *particular* machines happen to produce 842,
disassemble the executable and trace through it step by step keeping
track of everything that gets written to memory; somewhere along the
way you'll find that as a side effect of something else, 0x034a or
0x4a03 happens to get written to the location that j later occupies.