Re: srand(time(NULL))



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.

--
Eric.Sosman@xxxxxxx
.



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: srand(time(NULL))
    ... conversion of void * to whatever_type * is implicit). ... you should cast it to 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: 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: 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)