New to C, so forgive me if this is elementary, but I cannot come up with a successful search term on google.
I like to do a few problems on Project Euler when starting with a new language so I started on problem one and came across the problem.
my code...
#include <stdio.h>
int main()
{
int x;
int sum;
for (x = 0; x < 10; x++) {
if ( x % 3 == 0 || x % 5 == 0 ) {
sum = sum + x;
printf( "number is %d. sum is %d\n", x, sum );
}
}
printf( "The sum of the multiples of 3 and 5 below 1000 is %d\n", sum );
}
Now when I run this I get the output...
number is 0. sum is 32767
number is 3. sum is 32770
number is 5. sum is 32775
number is 6. sum is 32781
number is 9. sum is 32790
uhhhh.....I can see it is incrementing correclty, but why did the number start off ass 32767? Is it residing in my memory somewhere as that? Did some other program set a sum variable to that and it is now being called by this?
If that IS the case, then why does it still come out the same when I run MY program twice? it always seems to start at 32767
I solve it rather obviously by setting it as int sum = 0;
But I would like to understand what is happening in my original code.