On 20 Apr 2005 02:04:45 -0700, ccwork

> Keith Thompson wrote:
>> Here's a complete self-contained program (I've left the cast in
>> place):
>> #include
>> #include
>> int main(void)
>> {
>> unsigned long number = ULONG_MAX;
>> unsigned long result = 0;
>> result = (unsigned long) number % ULONG_MAX;
>> printf("ULONG_MAX=%lu, number=%lu, result=%lu\n",
>> ULONG_MAX, number, result);
>> return 0;
>> }
>> Save it to a file, compile it, and run it. If the output looks ok,
>> there may be some odd problem in your original code. If the problem
>> persists, post the exact output that you get. Be sure to
>> cut-and-paste it; don't attempt to re-type it.

> I try. In debug mode the result is:
> ULONG_MAX=4294967295, number=4294967295, result=0
> in release mode:
> ULONG_MAX=4294967295, number=4294967295, result=4294967295

It's a compiler optimisation bug of some sort. I just tried it with

Default (no) optimisation:

> cl 1.c
> 1.exe

ULONG_MAX=4294967295, number=4294967295, result=0

Maximum optimisation:

> cl /Ox 1.c
> 1.exe

ULONG_MAX=4294967295, number=4294967295, result=4294967295

The actual one which does it is /Og ("enable global optimisation"), the
other optimisation switches seem to be OK (on my machine).

Report it to MS, and/or ask in a MS newsgroup. If you want help with
it, email me privately (it's not on-topic for comp.lang.c, but I'm
reporting here so others know to stop looking, I've crossposted and set
followups to comp.os.ms-windows.programmer.misc where it's probably more

Chris C