D
Dave Sinkula
I have been told conflicting advice. Please offer comments regarding the
following.
=====
I visited Ben Pfaff's "When are casts appropriate?" page.
http://www.msu.edu/~pfaffben/writings/clc/casts.html
I mentioned to Ben Pfaff that a potential addition of an appropriate use of
a cast was when comparing to the result of the time() function (and another
instance not mentioned here).
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t now = time(NULL);
if ( now != (time_t)-1 )
{
fputs(ctime(&now), stdout);
}
return 0;
}
-----
The response I received from Ben Pfaff was as follows:
=====
Later, I passed this along and received this response:
-----
There isn't anything stopping an implementation from using, say, unsigned
char for time_t. Since the C standard specifies that time returns
(time_t)(-1), which would be promoted to 255 on many systems, the test
against unqualified -1 would fail to give correct results:
#include <stdio.h>
typedef unsigned char my_t;
int main ( void )
{
my_t t = (my_t)(-1);
if ( t == -1 )
printf ( "Equal\n" );
else
printf ( "Not equal\n" );
if ( t == (my_t)-1 )
printf ( "Equal\n" );
else
printf ( "Not equal\n" );
}
I don't know of any implementations where this would be an issue, but to be
strictly correct, the cast should be used. In fact, the standards committee
ended up correcting the mktime example in the standard when they realized
this because it originally used -1 without a cast.
-----
I relayed this to Ben Pfaff for comment, and he requested that I bring it up
here.
So here I am, and I believe the question is, "Can (or should) the value
returned by time() be compared to an unadorned -1, or is it appropriate to
use a cast and compare to (time_t)(-1)?"
following.
=====
I visited Ben Pfaff's "When are casts appropriate?" page.
http://www.msu.edu/~pfaffben/writings/clc/casts.html
I mentioned to Ben Pfaff that a potential addition of an appropriate use of
a cast was when comparing to the result of the time() function (and another
instance not mentioned here).
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t now = time(NULL);
if ( now != (time_t)-1 )
{
fputs(ctime(&now), stdout);
}
return 0;
}
-----
The response I received from Ben Pfaff was as follows:
type.I can't see why a cast is necessary there either; time_t is an arithmetic
=====
Later, I passed this along and received this response:
-----
There isn't anything stopping an implementation from using, say, unsigned
char for time_t. Since the C standard specifies that time returns
(time_t)(-1), which would be promoted to 255 on many systems, the test
against unqualified -1 would fail to give correct results:
#include <stdio.h>
typedef unsigned char my_t;
int main ( void )
{
my_t t = (my_t)(-1);
if ( t == -1 )
printf ( "Equal\n" );
else
printf ( "Not equal\n" );
if ( t == (my_t)-1 )
printf ( "Equal\n" );
else
printf ( "Not equal\n" );
}
I don't know of any implementations where this would be an issue, but to be
strictly correct, the cast should be used. In fact, the standards committee
ended up correcting the mktime example in the standard when they realized
this because it originally used -1 without a cast.
-----
I relayed this to Ben Pfaff for comment, and he requested that I bring it up
here.
So here I am, and I believe the question is, "Can (or should) the value
returned by time() be compared to an unadorned -1, or is it appropriate to
use a cast and compare to (time_t)(-1)?"