Bug Hunting Adventures #13: Prime Sums

“Why, yes; and not exactly that either. The fact is, we have all been a good deal puzzled because the affair is so simple, and yet baffles us altogether.”
― Edgar Allan Poe, The Purloined Letter

Below, you find a little C project that doesn’t do what it’s supposed to do, namely print the sum of the first 10 prime numbers. The program builds cleanly with gcc and clang; that is, without any warnings even when using -Wextra -Wall -pedantic -ansi as compiler options. It’s well-formed and doesn’t crash.

What’s the root cause of this bug? What’s the output of the program? Here are the files, you can also find them on GitHub:

prime_table.h:

prime_table.c:

prime_sum.c:

Makefile:

Solution

Comments (3)

  1. 13:02, 30-03-2019Shlomo  / Reply

    Yeah cause the array is filled in another file.
    The prime_table.h does not fill in the array.
    You’re defining the same variable 2 times.
    You can’t do this in c++.

  2. 13:05, 30-03-2019Shlomo  / Reply

    Also forgot ++i. It’s not counting for 2.

  3. 00:10, 31-03-2019Mark  / Reply

    It’s always the last place you’d expect, isn’t it? It seems like a good build system would at least warn you about that particular error.

Leave a Reply