Casts for srand? (FAQ 13.17)

M

Mara Guida

"Each time I run my program, I get the same sequence of numbers back
from rand()."


The answer to question 13.17, in the c-faq is:

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

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


Are all those casts in the srand() call really needed?
Is this ok, instead, relying on implicit casts when needed?

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

srand(time(NULL));
/* or even
srand(time(0));
*/


PS: According to the standard, does <stdlib.h> #define NULL?
 
D

dj3vande

"Each time I run my program, I get the same sequence of numbers back
from rand()."

The answer to question 13.17, in the c-faq is:

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

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

Are all those casts in the srand() call really needed?

No. They're probably there for historical reasons.

Is this ok, instead, relying on implicit casts when needed?

There's no such thing as an "implicit cast"; a cast is a source code
construct (and therefore inherently explicit) that forces a
conversion.
That nitpick aside, assuming you meant "implicit conversion", the
answer to your question is yes, it's perfectly valid.

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

srand(time(NULL));
/* or even
srand(time(0));
*/

If you forgot to #include <time.h>, you won't have a prototype in scope
for time(), and these would pass an integer 0 (or possibly a null void *)
instead of a null time_t *. Pre-ANSI C would have required a cast here
to force the right type to be passed, but now that we have prototypes
(and have for almost two decades) the correct solution is to
#include <time.h> and let the compiler do the conversion for you.

Casting the return value is slightly less useless, but only slightly;
time_t (which time() returns) is required to be an arithmetic type, and
every arithmetic type can be converted to unsigned without a cast, but
your compiler might warn about some of them and the cast will tell it
to be quiet (which is in general not such a good idea, but is probably
valid here).


dave
 
H

Harald van Dijk

"Each time I run my program, I get the same sequence of numbers back
from rand()."

The answer to question 13.17, in the c-faq is:

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

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

Are all those casts in the srand() call really needed? Is this ok,
instead, relying on implicit casts when needed?

A cast is an explicit conversion. An implicit cast would be an implicit
explicit conversion.

But no, there's nothing wrong with relying on the implicit conversion
from 0 or NULL to time_t *, and from time_t to unsigned int. Depending on
the implementation, the casts may affect some diagnostics, but no bugs
will be introduced or fixed by them.
PS: According to the standard, does <stdlib.h> #define NULL?

Yes. NULL is defined by <stddef.h>, <stdio.h>, <stdlib.h>, <string.h>,
<time.h>, <wchar.h>, and <locale.h>. It's harder to include standard
headers and _not_ get NULL defined, than it is to accidentally leave it
undefined.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top