Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6796949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:32:57+00:00 2026-05-26T18:32:57+00:00

I’m learning the use of the switch statement and using the rand() and srand()

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:32:57+00:00Added an answer on May 26, 2026 at 6:32 pm

    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 #includeing 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.