Re: srand(time(NULL))




"Keith Thompson" <kst-u@xxxxxxx> wrote in message
news:87r6eio8a9.fsf@xxxxxxxxxxxxxxxxxx
Eric Sosman <Eric.Sosman@xxxxxxx> writes:
Ioannis Vranos wrote:
The FAQ mentions:

#include <stdlib.h>
#include <time.h>

srand((unsigned int)time((time_t *)NULL));


I think the casting of NULL to "time_t *" is unnecessary in C, since
NULL can be assigned to any pointer type without casting (also the
conversion of void * to whatever_type * is implicit).


Also since the srand() function protype is

void srand(unsigned int seed);


I think the casting of the return value of time() to unsigned int is
unnecessary, since the return value is implicitly converted to the
unsigned int argument.

"Unnecessary" is not always the same as "undesirable" --
do you use indentation to indicate block nesting? -- and the
question of whether to use or omit these casts is in some
degree a matter of taste. Myself, I'd omit them. Yet the
Ten Commandments For C Programmers takes the opposite view
(Third Commandment), so I am probably in a state of sin.

Reference: <http://www.lysator.liu.se/c/ten-commandments.html>,
written by Henry Spencer.

The original version of this fine document was written, I believe,
before the existence of ANSI C. The Third Commandment is:

Thou shalt cast all function arguments to the expected type if
they are not of that type already, even when thou art convinced
that this is unnecessary, lest they take cruel vengeance upon thee
when thou least expect it.

The newer annotation reads, in part:

It may be thought that the radical new blessing of ``prototypes''
might eliminate the need for caution about argument types. Not so,
brethren. Firstly, when confronted with the twisted strangeness of
variable numbers of arguments, the problem returns... and he who
has not kept his faith strong by repeated practice shall surely
fall to this subtle trap. Secondly, the wise men have observed
that reliance on prototypes doth open many doors to strange
errors, and some indeed had hoped that prototypes would be decreed
for purposes of error checking but would not cause implicit
conversions. Lastly, reliance on prototypes causeth great
difficulty in the Real World today, when many cling to the old
ways and the old compilers out of desire or necessity, and no man
knoweth what machine his code may be asked to run on tomorrow.

This was good advice *at the time it was written*. Today, C compilers
that don't support prototypes are vanishingly rare, and this advice is
IMHO obsolete. You still need to be careful when calling variadic
functions; for example, when passing a pointer to printf with the "%p"
format, you should cast it to void*. (Quibbles: If it's already
void*, you don't need to cast it; if it's char*, you can probably get
away without casting it, but you should anyway; if it's a function
pointer, you can't portably print it with "%p" with or without a
cast.)

snip

Keith this wouldn've worked before c99. What did people do before C99 ? In
C89 if I'm right there was no time.h or time_t type. This answers a previous
question of mine.

Bill


.



Relevant Pages

  • Re: gcc knows about malloc()
    ... If a correct prototype is visible, this expression (after the implicit ... actual conversion and simply yields the same pointer-to-function ... this value to type void *. ...
    (comp.lang.c)
  • Re: cast to void *
    ... When else is implicit conversion to 'void *' done, ... Except that conversion between void* and struct tm is a constraint ...
    (comp.lang.c)
  • Re: malloc and free
    ... There's no such thing as an implicit ... > A conversion is an action that occurs during program execution. ... > can be either explicit (i.e., specified by a cast operator in the ... It specifies an explicit conversion. ...
    (comp.lang.c)
  • Re: malloc and free
    ... No, it's an implicit conversion. ... A conversion is an action that occurs during program execution. ... specified by a cast operator in the ... It specifies an explicit conversion. ...
    (comp.lang.c)
  • Re: srand(time(NULL))
    ... NULL can be assigned to any pointer type without casting (also the conversion of void * to whatever_type * is implicit). ... I think the casting of the return value of timeto unsigned int is unnecessary, since the return value is implicitly converted to the unsigned int argument. ...
    (comp.lang.c)

Loading