Having such a simple C++ code:
//...
DWORD time1 = 12;
DWORD time2 = 1;
DWORD timeDelta = time2 - time1;
if (timeDelta == -11) {
swprintf_s(g_msgbuf, L"equality 1\n");
OutputDebugString(g_msgbuf);
}
if (timeDelta == 4294967285) {
swprintf_s(g_msgbuf, L"equality 2\n");
OutputDebugString(g_msgbuf);
}
//...
I'm getting BOTH outputs(both conditions fulfilled):
equality 1
equality 2
I know that timeDelta
variable when treated as signed 'll be -11
and as unsigned as 4294967285
BUT i'ts so strange to me. Such an ambiguity may lead to serious problems in the code later. Since the timeDelta
variable is of type DWORD
which means unsigned long
should't the ==
operator take it into consideration and then fulfilled ONLY in the second case (if (timeDelta == 4294967285)...
)!?
In my opinion it SHOULD be absolutely NECESSARILY to EXLPICITELLY use the type casting like: if ((singed long)timeDelta == -11)...
to make the condition fulfilled!?