Crank up them warnings:
Code:
$ gcc -Wall foo.c
foo.c:5: warning: return type defaults to ‘int’
foo.c: In function ‘main’:
foo.c:8: warning: implicit declaration of function ‘srand’
foo.c:8: warning: implicit declaration of function ‘time’
foo.c:9: warning: implicit declaration of function ‘rand’
foo.c:31: warning: control reaches end of non-void function
You need to include time.h and stdlib.h for the correct prototypes for srand() and time(). Then, your compiler should warn you:
Code:
$ gcc -Wall foo.c
foo.c:7: warning: return type defaults to ‘int’
foo.c: In function ‘main’:
foo.c:10: error: too few arguments to function ‘time’
You need to pass in a pointer to a struct tm into time, or better yet, pass NULL for the current time. Also, it's int main(void) and return an integer at the end, usually zero.
EDIT: The reason for the seg fault was the lack of parameter to time(). The time function was grabbing whatever garbage was on the stack, which was mostly likely not a NULL address, thus trying to dereference invalid memory.