/dev/geek said:
Just thought I'd mention a couple things...
Since time() returns -1 on failure, we need to do some error checking:
time_t rt;
if ((rt = time(NULL)) == -1) {
It will probably never make any difference, but the
pedant-approved way to write this test is
if ((rt = time(NULL)) == (time_t)-1) {
Here's why: We do not actually know what hides behind
the `time_t' mask; all we know is that it is an arithmetic
type capable of representing times. "Arithmetic type" could
be any signed or unsigned integer type or even a floating-point
type; in C99 I suppose it could be a complex type. (Hawking
has argued on cosmological grounds that time is complex.)
Now, suppose `time_t' is an unsigned integer type that
is narrower than `int' -- `unsigned char' or `unsigned short'
or one of C99's extended types, say. Then if time() fails,
rt will have a positive narrower-than-`int' value (it will
be sometype_MAX), and this value will remain positive after
being promoted for comparison with the `int' value -1. The
comparison will report "unequal" and the failure will go
undetected.
Admittedly, this is all pretty far-fetched. The original
reason time() took an argument was that the "returned" value
was too large for the 16-bit `int' of the era and `long' hadn't
been invented yet, so it's unlikely that you'll ever find a
`time_t' that's narrower than `int'. Indeed, some systems are
beginning to adopt a `time_t' that's wider than `long'! Still,
there's always the remote possibility that your code might need
to run on the DeathStation 9000, where `time_t' occupies three
eleven-bit bytes and an `int' occupies five ...