Reputation: 1990
I'm learning the use of the switch statement and using the rand() and srand() functions but then I'm getting a segmentation fault when I try to run this code I got from this book I'm learning C from. What's could be causing this to happen?
#include <stdio.h>
int main(void)
{
int iRandomNum = 0;
srand(time());
iRandomNum = (rand() % 4) + 1;
printf("\nFortune Cookie - Chapter 3\n");
switch (iRandomNum) {
case 1:
printf("\nYou will meet a new friend today.\n");
break;
case 2:
printf("\nYou will enjoy a long and happy life.\n");
break;
case 3:
printf("\nOpportunity knocks softly. Can you hear it?\n");
break;
case 4:
printf("\nYou'll be financially rewarded for your good deeds.\n");
break;
} //end switch
printf("\nLucky lotto numbers: ");
printf("%d ", (rand() % 49) + 1);
printf("%d ", (rand() % 49) + 1);
printf("%d ", (rand() % 49) + 1);
printf("%d ", (rand() % 49) + 1);
printf("%d ", (rand() % 49) + 1);
printf("%d\n", (rand() % 49) + 1);
} //end main function
Upvotes: 0
Views: 4238
Reputation: 263177
You're calling time()
with no arguments. The time()
function takes one argument, and it's not optional.
Add
#include <time.h>
to the top of your source file, and change the srand()
call to
srand(time(NULL));
EDIT :
You also need to add
#include <stdlib.h>
to get the declarations of srand()
and rand()
.
EDIT 2:
You can often (seemingly) get away with calling a function without #include
ing the header in which it's declared.
In C90, if you call a function with no visible declaration, the compiler implicitly creates a declaration for it, assuming that the function returns int
and takes the arguments given in the call. Both srand()
and rand()
do in fact return int
results, so calls to them could work. But time()
takes an argument of type time_t*
, and returns a result of type time_t
; a call with no declaration might work if you're "lucky", or it might blow up in your face.
The 1999 ISO C standard (C99) changed the rules, so calling a function without a visible declaration is a constraint violation, requiring a diagnostic from the compiler.
Even in C90 mode, most compilers can be persuaded to warn about undeclared functions if you give them the right options.
Bottom line: If you're going to call a library function, read its documentation (man page, whatever) and add a #include
for the header that declares it. And don't count on the compiler to remind you if you forget to do this.
Upvotes: 2
Reputation: 298046
I used gdb
to debug your code and your segfault happens at this line:
srand(time());
You are using time()
incorrectly. You need to pass a parameter that will hold the resulting time, but NULL
will cause the function to return
the time:
srand(time(NULL));
Also, make sure to always return
from the main()
function.
This code works fine:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
int iRandomNum = 0;
srand(time(NULL));
iRandomNum = (rand() % 4) + 1;
printf("\nFortune Cookie - Chapter 3\n");
switch (iRandomNum) {
case 1:
printf("\nYou will meet a new friend today.\n");
break;
case 2:
printf("\nYou will enjoy a long and happy life.\n");
break;
case 3:
printf("\nOpportunity knocks softly. Can you hear it?\n");
break;
case 4:
printf("\nYou'll be financially rewarded for your good deeds.\n");
break;
} //end switch
printf("\nLucky lotto numbers: ");
printf("%d ", (rand() % 49) + 1);
printf("%d ", (rand() % 49) + 1);
printf("%d ", (rand() % 49) + 1);
printf("%d ", (rand() % 49) + 1);
printf("%d ", (rand() % 49) + 1);
printf("%d\n", (rand() % 49) + 1);
return 0;
} //end main function
Just in case you're interested in how I debugged your code, I compiled it with debugging symbols and every compiler warning possible:
gcc -g -Wall -Wextra test.c -o test
And I started gdb
:
gdb ./test
(gdb) run
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7ffba2c in time ()
(gdb) quit
Upvotes: 3
Reputation: 881093
It's because time()
requires an argument, something the compiler would tell if you turned on all warnings, such as with gcc -Wall -Wextra ...
.
The combination of not including time.h
(meaning that time()
gets a default prototype) and calling it without an argument is your specific problem.
The full list of problems found when using higehr warning levels are:
time()
, srand()
and rand()
have no prototypes, you need to #include
both stdlib.h
and time.h
.time()
requires an argument, like in srand (time (0))
.main
(not strictly necessary in very recent iterations of the language but still good practice).The following changes work fine:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main (void) {
int iRandomNum = 0;
srand (time (0));
iRandomNum = (rand() % 4) + 1;
printf("\nFortune Cookie - Chapter 3\n");
switch (iRandomNum) {
case 1:
printf("\nYou will meet a new friend today.\n");
break;
case 2:
printf("\nYou will enjoy a long and happy life.\n");
break;
case 3:
printf("\nOpportunity knocks softly. Can you hear it?\n");
break;
case 4:
printf("\nYou'll be financially rewarded for your good deeds.\n");
break;
} //end switch
printf("\nLucky lotto numbers: ");
printf("%d ", (rand() % 49) + 1);
printf("%d ", (rand() % 49) + 1);
printf("%d ", (rand() % 49) + 1);
printf("%d ", (rand() % 49) + 1);
printf("%d ", (rand() % 49) + 1);
printf("%d\n", (rand() % 49) + 1);
return 0;
}
Upvotes: 5