j0******@engineer.com (j0mbolar) wrote:
I was looking over some code and came across this:
new_time = mktime(&tm);
if(new_time == (time_t)-1)
goto err;
is this cast necessary in a strictly conforming
program?
I think so. If time_t is a small type, the comparison (new_time == -1)
will result in new_time being promoted to int, and then compared to -1;
if time_t is also unsigned, this comparison will be false even if
new_time is actually equal to (time_t)-1.
For this to happen, time_t must be unsigned short, unsigned char, or a
small C99 unsigned type, and the range of that type must be smaller than
that of unsigned int. This is, of course, not very likely, but when
you're after strict conformance, "unlikely but legal" is bad enough.
Richard