1

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.

Joff
  • 11,247
  • 16
  • 60
  • 103
  • 1
    What did you expect to happen? – M.M Apr 21 '16 at 04:35
  • LIke I said I am new to C so I have never dealt with these issues before. I know it has something to do with memory and not setting the variable, but I am asking what is happening behind the scenes – Joff Apr 21 '16 at 04:36
  • Well you should have some sort of expectation about what your code will do .. – M.M Apr 21 '16 at 04:41
  • I expected it to work from 0. Seeing the results I can see that I needed to set it to 0, how I could expect 32797 to come out? IDK – Joff Apr 21 '16 at 04:47
  • It won't always be 32797, by the way. – user253751 Apr 21 '16 at 05:25

3 Answers3

4

Unlike some other programming languages like C# and Java, C does not automatically initialize its variables, meaning that you will see whatever random data happens to be at the memory location that is assigned to the variable when the variable is created.

For this reason, you must always set your variables to some initial value before using them (usually zero for numeric variables).

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • That is exactly what I am looking for. Thank you – Joff Apr 21 '16 at 04:38
  • 1
    Automatic storage isn't initialised but static storage is. – teppic Apr 21 '16 at 04:40
  • 1
    @teppic: A fact that should become readily apparent to the OP as he studies more closely the C specification. It's good practice to get into the habit of manually initializing every variable anyway. – Robert Harvey Apr 21 '16 at 04:41
  • one more question adding onto your explanation, I ran m code a few times. why did it not start out as 32790 (the last number in my sequence) the second time it ran. Why did it reset to 32767? – Joff Apr 21 '16 at 04:50
  • 2
    32767 is the largest possible 16 bit signed integer (all bits are set to 1, except the sign bit). Some compilers set sentinel values for uninitialized variables, so that you can clearly see that they're uninitialized in a debugger. – Robert Harvey Apr 21 '16 at 04:51
  • 1
    uninitialized memory is often set to [0xCC in debug mode](http://stackoverflow.com/q/370195/995714) – phuclv Apr 21 '16 at 05:27
1

Uninitialized variables can contain any value(32767 in your case), or even trap representation. In fact, trying accessing an uninitialized invokes undefined behavior, which makes everything from working as expected to a runtime crash possible.

Since sum is expected to start at 0, initialize it as following works:

int sum = 0;
nalzok
  • 14,965
  • 21
  • 72
  • 139
0

Local variables declared inside a function are not initialized by the runtime. They are allocated on the stack and have undefined values (whatever happens to be in that stack location).

Variables defined in global scope are initialized to 0 automatically.

Sami Sallinen
  • 3,203
  • 12
  • 16