Originally posted by pr0ficient
I thought that if you assigned an int within a function it is automatically set to 0.
Nope. The only variables that are automaticly initalized are 'static' variables, and then even with those, it's only at the time of creation, not every time the function is called.
Without you actually initializing a variable, it just hold whatever random value that bit of memory held before it was set aside for that variable.
For example:
Code:
#include <stdio.h>
int myfun1( void ) { int x; return x; }
int myfun2( void ) { int x; return x; }
int myfun3( void ) { int x; return x; }
int myfun4( void ) { int x; return x; }
int mystaticfun( void ) { int x; return x; }
int main( void )
{
return printf("%d %d %d %d, and static: %d\n",
myfun1(), myfun2(), myfun3(), myfun4(), mystaticfun() );
}
If any of the first 4 are zero, it's only luck or your specific compiler.
Quzah.