By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

Segmentation Fault using the srand function

P: n/a
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);

}

}

Jun 27 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a


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.)

--
Er*********@sun.com

Jun 27 '06 #2

P: n/a
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.)

--
Er*********@sun.com


Jun 27 '06 #3

P: n/a
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)
Jun 27 '06 #4

P: n/a
On Tue, 27 Jun 2006 17:34:13 +0000, Richard Heathfield
<in*****@invalid.invalid> 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/~a...FAQ-acllc.html
Jun 27 '06 #5

P: n/a
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)


Jun 27 '06 #6

P: n/a
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)
Jun 27 '06 #7

P: n/a
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)


Jun 27 '06 #8

P: n/a
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.
Jun 27 '06 #9

P: n/a
On 27 Jun 2006 11:16:52 -0700, "jtagpgmr" <ru************@gmail.com>
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/~a...FAQ-acllc.html
Jun 27 '06 #10

P: n/a
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" <ru************@gmail.com>
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/~a...FAQ-acllc.html


Jun 27 '06 #11

P: n/a
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

Jun 27 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.