srand(time()) returns segmentation fault
Hey everyone,
Here's another problem I encountered:
when I write this: (This is a beginning of a "guess a number game")
Code:
#include <stdio.h>
main()
{
int iRandomNum = 0;
int iResponse = 0;
srand(time());
iRandomNum = (rand() % 10) + 1;
...
it returns a segmentation fault. I tested multiple instances, and what I observed is, whenever I define a variable as (rand() % 10) + 1 following srand(time()) it doesn't work and I'm not sure why.
This however works:
Code:
#include <stdio.h>
main()
{
srand(time());
printf("\n%d\n", (rand() % 8) + 1);
}
The first code snippet is provided by the book "C programming for the absolute beginner"
Thanks for all the help