1

These snippets, respectively written in C and Java should output the same result, but they do not, and I'm not able to identify where's the bug.

written in C

#include <stdio.h>
/* discover and print  all the multiples of 3 or 5
below 1000 */

int main() {

    int sum, counter = 1;

    while (counter < 1000) {
        printf("Calculating...\n");
        printf("%d numbers already verified.\n", counter); 
        if ( counter % 3 == 0 || counter % 5 == 0 ) {
            sum += counter;
        }
        ++counter;  
    }

    printf("The sum of all multiples is: %d", sum);
    return 0;
}

Java:

package problems;
//Prints the sum of all the multiples of 3 or five below 1000

public class Problem1 {
    public static void main(String[] args) {
        int sum = 0, counter = 1;

        while (counter < 1000) {
            System.out.format("Calculating...%n");
            System.out.format("%d numbers already verified.%n",counter);
            if( (counter % 3 == 0) || (counter % 5 == 0) ) {
                sum += counter;
            }
            ++counter;
        }
        System.out.format("The sum of all multiples is: %d", sum);
    }
}

C outputs 2919928 as the total sum, while Java outputs 233168.

Ezequiel Barbosa
  • 211
  • 1
  • 15
  • 12
    You never initialized the value of sum in the C code. You should always compile C with `-Wall`. The correct sum is indeed 233168. – moveaway00 Apr 19 '15 at 03:48
  • @moveaway00, curious, any thoughts on why not initializing the variable in C results with that sum? – jwilner Apr 19 '15 at 03:56
  • 1
    @jwilner My guess is that the value of sum, when not initialized, is undefined. Therefore sum will take the value of what is currently in memory, which produces inaccuracies like this. – Corb3nik Apr 19 '15 at 04:01
  • 3
    Because in C unintialised local (stack) variables can have any random value. So when you do `sum+=counter` you are really doing `sum=+counter`. – kaylum Apr 19 '15 at 04:01
  • The difference between the result you get and the real result happens to be 0x28FF28. My guess is that your compiler fills your stack with this value as a sentinel value in case you leave things uninitialized. Just a guess though. You should be glad you got a number at all, relying on the value of an unitialized variable is Undefined Behavior, so the compiler would be free to do what ever it wanted. [nasal demons](http://www.catb.org/jargon/html/N/nasal-demons.html) included. – moveaway00 Apr 19 '15 at 04:02
  • 2
    @moveaway00 No, the compiler doesn't fill the stack with any values. That would be waste of cpu cycles. Stack memory will just contain whatever happens to be in there from the last time it was used. – kaylum Apr 19 '15 at 04:05
  • @AlanAu it _will_ be initialized data, the OS doesn't just give you memory with old data from other programs laying around. That would be a HUGE security issue. Since this is the main function, we can assume it's not junk data from previous stack frames. – moveaway00 Apr 19 '15 at 04:12
  • 1
    @moveaway00 The OS is not the compiler. So yes, the OS does set up the stack with initialised (zero) data. But not the compiler. And by the time main runs, the stack has already been used many times. There is code run before main. – kaylum Apr 19 '15 at 04:17
  • I think it isn't a random value, because I compiled the code more than once, and even after turning the computer off it still gives that value. Anyway, it doesn't really matter, and thanks for the heads up, I will initialize sum as 0. – Ezequiel Barbosa Apr 19 '15 at 15:07

2 Answers2

2

The problem in your code is that you are not initializing the variable sum.

The value of sum, when not initialized, is undefined. Therefore sum will take the value of what is currently in memory, which produces inaccuracies like this.

Intialize the variable sum to 0 and you should get the correct result.

Corb3nik
  • 1,177
  • 7
  • 26
2

In the C code you've written

int sum, counter = 1;

This means that the sum is not initialized with a value.
Unlike in Java the defult value for an int is not zero in C. Take a look at the question here for more details.

This value could be a garbage value and your code will add to that value instead of zero giving an invalid result.

To fix your code simply initialize sum when declaring the variable.

 int sum = 0;
 int counter = 1; 
Community
  • 1
  • 1
nipuna-g
  • 6,252
  • 3
  • 31
  • 48