Segmentation Fault using the srand function

Discussion in 'C Programming' started by jtagpgmr, Jun 27, 2006.

  1. jtagpgmr

    jtagpgmr Guest

    I am currently using the gcc compiler on a cygwin platform, I am a
    beginner when it comes to programming in C and want to know why anytime
    I run the .exe with the following code I get a "segmentation fault
    (core dumped)" error:

    #include <stdio.h>

    main()

    {

    int x, iNumQuestions, iResponse, iRndNum1, iRndNum2;
    srand(time());

    printf("\nEnter number of questions to ask: ");
    scanf("%d", &iNumQuestions);

    for (x=0; x<iNumQuestions; x++) {


    iRndNum1=rand()%10+1;
    iRndNum2=rand()%10+1;

    printf("\nWhat is %d x %d: ", iRndNum1, iRndNum2);
    scanf("%d", &iResponse);

    if (iResponse == iRndNum1*iRndNum2)
    printf("\nCorrect!\n");

    else
    printf("\nThe correct answer was %d \n", iRndNum1*iRndNum2);

    }

    }
    jtagpgmr, Jun 27, 2006
    #1
    1. Advertising

  2. jtagpgmr

    Eric Sosman Guest

    jtagpgmr wrote On 06/27/06 13:14,:
    > I am currently using the gcc compiler on a cygwin platform, I am a
    > beginner when it comes to programming in C and want to know why anytime
    > I run the .exe with the following code I get a "segmentation fault
    > (core dumped)" error:
    >
    > #include <stdio.h>
    >
    > main()
    >
    > {
    >
    > int x, iNumQuestions, iResponse, iRndNum1, iRndNum2;
    > srand(time());
    > [...]


    This is the proximate cause of your problem: The time()
    function requires an argument, but you didn't provide one.

    Do not imagine that this is the only error in your
    code. To learn about the others, ask gcc to be more verbose
    in its diagnostics: Add "-W -Wall" to the other command-line
    flags, and pay attention to what the compiler then tells you.
    (If you're really brave, use "-W -Wall -ansi -pedantic" or
    "-W -Wall -std=c99 -pedantic". It will be a learning
    experience.)

    --
    Eric Sosman, Jun 27, 2006
    #2
    1. Advertising

  3. jtagpgmr

    jtagpgmr Guest

    What does the following mean?
    implicit declaration of function srand
    implicit declaration of function time
    implicit declaration of function rand

    Eric Sosman wrote:
    > jtagpgmr wrote On 06/27/06 13:14,:
    > > I am currently using the gcc compiler on a cygwin platform, I am a
    > > beginner when it comes to programming in C and want to know why anytime
    > > I run the .exe with the following code I get a "segmentation fault
    > > (core dumped)" error:
    > >
    > > #include <stdio.h>
    > >
    > > main()
    > >
    > > {
    > >
    > > int x, iNumQuestions, iResponse, iRndNum1, iRndNum2;
    > > srand(time());
    > > [...]

    >
    > This is the proximate cause of your problem: The time()
    > function requires an argument, but you didn't provide one.
    >
    > Do not imagine that this is the only error in your
    > code. To learn about the others, ask gcc to be more verbose
    > in its diagnostics: Add "-W -Wall" to the other command-line
    > flags, and pay attention to what the compiler then tells you.
    > (If you're really brave, use "-W -Wall -ansi -pedantic" or
    > "-W -Wall -std=c99 -pedantic". It will be a learning
    > experience.)
    >
    > --
    >
    jtagpgmr, Jun 27, 2006
    #3
  4. jtagpgmr said:

    > What does the following mean?
    > implicit declaration of function srand


    It means you forgot to #include <stdlib.h>

    > implicit declaration of function time


    It means you forgot to #include <time.h>

    > implicit declaration of function rand


    It means you forgot to #include <stdlib.h>

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Jun 27, 2006
    #4
  5. jtagpgmr

    Jack Klein Guest

    On Tue, 27 Jun 2006 17:34:13 +0000, Richard Heathfield
    <> wrote in comp.lang.c:

    > jtagpgmr said:
    >
    > > What does the following mean?
    > > implicit declaration of function srand

    >
    > It means you forgot to #include <stdlib.h>
    >
    > > implicit declaration of function time

    >
    > It means you forgot to #include <time.h>
    >
    > > implicit declaration of function rand

    >
    > It means you forgot to #include <stdlib.h>


    ....and had you included those headers, the compiler would have warned
    you immediately that you were calling the time() function incorrectly.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jun 27, 2006
    #5
  6. jtagpgmr

    jtagpgmr Guest

    Thanks for the help the follwing was wrong..
    added: #include <stdlib.h>

    omitted: srand(time())
    inserted: srand(rand())

    The funny thing is this program excercise was taken straight from a
    book..

    Richard Heathfield wrote:
    > jtagpgmr said:
    >
    > > What does the following mean?
    > > implicit declaration of function srand

    >
    > It means you forgot to #include <stdlib.h>
    >
    > > implicit declaration of function time

    >
    > It means you forgot to #include <time.h>
    >
    > > implicit declaration of function rand

    >
    > It means you forgot to #include <stdlib.h>
    >
    > --
    > Richard Heathfield
    > "Usenet is a strange place" - dmr 29/7/1999
    > http://www.cpax.org.uk
    > email: rjh at above domain (but drop the www, obviously)
    jtagpgmr, Jun 27, 2006
    #6
  7. jtagpgmr said:

    > Thanks for the help the follwing was wrong..
    > added: #include <stdlib.h>
    >
    > omitted: srand(time())


    Why not just *fix* it instead of removing it?

    > inserted: srand(rand())


    Why would you want to do /that/?

    > The funny thing is this program excercise was taken straight from a
    > book..


    Highly amusing.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Jun 27, 2006
    #7
  8. jtagpgmr

    jtagpgmr Guest

    Is there something else I could have done..?? Im sort of confused..
    Richard Heathfield wrote:
    > jtagpgmr said:
    >
    > > Thanks for the help the follwing was wrong..
    > > added: #include <stdlib.h>
    > >
    > > omitted: srand(time())

    >
    > Why not just *fix* it instead of removing it?
    >
    > > inserted: srand(rand())

    >
    > Why would you want to do /that/?
    >
    > > The funny thing is this program excercise was taken straight from a
    > > book..

    >
    > Highly amusing.
    >
    > --
    > Richard Heathfield
    > "Usenet is a strange place" - dmr 29/7/1999
    > http://www.cpax.org.uk
    > email: rjh at above domain (but drop the www, obviously)
    jtagpgmr, Jun 27, 2006
    #8
  9. jtagpgmr

    Simon Biber Guest

    jtagpgmr wrote:
    > Thanks for the help the follwing was wrong..
    > added: #include <stdlib.h>


    You should also add #include <time.h> if you want to continue using the
    time function.

    > omitted: srand(time())


    This can be fixed by supplying a null pointer argument to the time function:
    srand(time(0));

    This will work if time(0) returns a number that has a reasonable range
    of different values when converted to an unsigned int. This is far from
    guaranteed by the C Standard but tends to work in practise.

    > inserted: srand(rand())


    That is useless. The whole reason you wanted to change the seed value
    was to get a different set of random numbers each time you ran the
    program. Now the rand() function will return the same number each time
    you run the program, which goes into the seed, and then the following
    sequence will also be the same.

    > The funny thing is this program excercise was taken straight from a
    > book..


    Either they, or you, left out the 0 or NULL from the call to time.

    --
    Simon.
    Simon Biber, Jun 27, 2006
    #9
  10. jtagpgmr

    Jack Klein Guest

    On 27 Jun 2006 11:16:52 -0700, "jtagpgmr" <>
    wrote in comp.lang.c:

    > Thanks for the help the follwing was wrong..
    > added: #include <stdlib.h>
    >
    > omitted: srand(time())
    > inserted: srand(rand())
    >
    > The funny thing is this program excercise was taken straight from a
    > book..
    >
    > Richard Heathfield wrote:
    > > jtagpgmr said:
    > >
    > > > What does the following mean?
    > > > implicit declaration of function srand

    > >
    > > It means you forgot to #include <stdlib.h>
    > >
    > > > implicit declaration of function time

    > >
    > > It means you forgot to #include <time.h>
    > >
    > > > implicit declaration of function rand

    > >
    > > It means you forgot to #include <stdlib.h>


    Get a better book. http://www.accu.org is one place to look for good
    books on C programming. There are a lot of bad books out there, in
    fact the great majority of them.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jun 27, 2006
    #10
  11. jtagpgmr

    jtagpgmr Guest

    ahh... got it.. that works just fantastic.. Thanks final draft

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

    main()

    {

    int x, iNumQuestions, iResponse, iRndNum1, iRndNum2;
    srand(time(0));

    printf("\nEnter number of questions to ask: ");
    scanf("%d", &iNumQuestions);

    for (x=0; x<iNumQuestions; x++) {


    iRndNum1=rand()%10+1;
    iRndNum2=rand()%10+1;

    printf("\nWhat is %d x %d: ", iRndNum1, iRndNum2);
    scanf("%d", &iResponse);

    if (iResponse == iRndNum1*iRndNum2)
    printf("\nCorrect!\n");

    else
    printf("\nThe correct answer was %d \n", iRndNum1*iRndNum2);

    }

    }

    Jack Klein wrote:
    > On 27 Jun 2006 11:16:52 -0700, "jtagpgmr" <>
    > wrote in comp.lang.c:
    >
    > > Thanks for the help the follwing was wrong..
    > > added: #include <stdlib.h>
    > >
    > > omitted: srand(time())
    > > inserted: srand(rand())
    > >
    > > The funny thing is this program excercise was taken straight from a
    > > book..
    > >
    > > Richard Heathfield wrote:
    > > > jtagpgmr said:
    > > >
    > > > > What does the following mean?
    > > > > implicit declaration of function srand
    > > >
    > > > It means you forgot to #include <stdlib.h>
    > > >
    > > > > implicit declaration of function time
    > > >
    > > > It means you forgot to #include <time.h>
    > > >
    > > > > implicit declaration of function rand
    > > >
    > > > It means you forgot to #include <stdlib.h>

    >
    > Get a better book. http://www.accu.org is one place to look for good
    > books on C programming. There are a lot of bad books out there, in
    > fact the great majority of them.
    >
    > --
    > Jack Klein
    > Home: http://JK-Technology.Com
    > FAQs for
    > comp.lang.c http://c-faq.com/
    > comp.lang.c++ http://www.parashift.com/c -faq-lite/
    > alt.comp.lang.learn.c-c++
    > http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    jtagpgmr, Jun 27, 2006
    #11
  12. jtagpgmr

    Default User Guest

    jtagpgmr wrote:

    > What does the following mean?




    Please don't top-post. Your replies belong following or interspersed
    with properly trimmed quotes. See the other 99% of the posts in this
    group or <http://www.caliburn.nl/topposting.html (thanks Keith).




    Brian
    Default User, Jun 27, 2006
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Strasser

    Segmentation fault when function ends...

    Stefan Strasser, Mar 30, 2005, in forum: C++
    Replies:
    14
    Views:
    1,615
    Victor Bazarov
    Mar 30, 2005
  2. Chris Van Extergem

    Function pointer results in segmentation fault.

    Chris Van Extergem, May 1, 2006, in forum: C Programming
    Replies:
    5
    Views:
    852
    Chris Van Extergem
    May 2, 2006
  3. Replies:
    1
    Views:
    414
  4. eliweiq001
    Replies:
    12
    Views:
    801
    Keith Thompson
    Oct 6, 2010
  5. Arijit Das

    srand versus srandom - srand with random() safe?

    Arijit Das, Oct 17, 2011, in forum: C Programming
    Replies:
    12
    Views:
    2,954
    Nick Keighley
    Oct 18, 2011
Loading...

Share This Page