Given the following program:
/* Find the sum of all the multiples of 3 or 5 below 1000. */
#include <stdio.h>
unsigned long int method_one(const unsigned long int n);
int
main(int argc, char *argv[])
{
unsigned long int sum = method_one(1000000000);
if (sum != 0) {
printf("Sum: %lu\n", sum);
} else {
printf("Error: Unsigned Integer Wrapping.\n");
}
return 0;
}
unsigned long int
method_one(const unsigned long int n)
{
unsigned long int i;
unsigned long int sum = 0;
for (i=1; i!=n; ++i) {
if (!(i % 3) || !(i % 5)) {
unsigned long int tmp_sum = sum;
sum += i;
if (sum < tmp_sum)
return 0;
}
}
return sum;
}
On a Mac OS system (Xcode 3.2.3) if I use cc
for compilation using the -std=c99
flag everything seems just right:
nietzsche:problem_1 robert$ cc -std=c99 problem_1.c -o problem_1
nietzsche:problem_1 robert$ ./problem_1
Sum: 233333333166666668
However, if I use c99
to compile it this is what happens:
nietzsche:problem_1 robert$ c99 problem_1.c -o problem_1
nietzsche:problem_1 robert$ ./problem_1
Error: Unsigned Integer Wrapping.
Can you please explain this behavior?
Under Mac OS X, cc is symlink to gcc (defaults to 64 bit), and c99 is not (defaults to 32bit).
/usr/bin/cc -> gcc-4.2
And they use different default byte-sizes for data types.
Quite simply, you are overflowing (aka wrapping) your integer variable when using the c99 compiler.
.PMCD.
c99
is a wrapper ofgcc
. It exists because POSIX requires it.c99
will generate a 32-bit (i386) binary by default.cc
is a symlink togcc
, so it takes whatever default configurationgcc
has.gcc
produces a binary in native architecture by default, which is x86_64.unsigned long
is 32-bit long on i386 on OS X, and 64-bit long on x86_64. Therefore,c99
will have a "Unsigned Integer Wrapping", whichcc -std=c99
does not.You could force
c99
to generate a 64-bit binary on OS X by the-W 64
flag.(Note: by
gcc
I mean the actual gcc binary likei686-apple-darwin10-gcc-4.2.1
.)