473,805 Members | 2,119 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Segmentation Fault using the srand function

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 "segmentati on 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*iRndNu m2)
printf("\nCorre ct!\n");

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

}

}

Jun 27 '06 #1
11 7722


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 "segmentati on 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
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 "segmentati on 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
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
On Tue, 27 Jun 2006 17:34:13 +0000, Richard Heathfield
<in*****@invali d.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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jun 27 '06 #5
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
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
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
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
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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jun 27 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6816
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script works fine and do all what I need. But at the end of the execution, I can read "Segmentation Fault". The segmentation fault appear at the end of my script execution,
5
2999
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I understood that a segmentation fault can occur whenever I declare a pointer and I leave it un-initialized. So I thought the problem here is with the (const char *)s in the stuct flightData (please note that I get the same fault declaring as char * the...
6
2790
by: tigrfire | last post by:
I've been working on a program to try and play a game of Craps, based on a version I found elsewhere - I didn't code the original, but I added a few things such as a balance and wager system. I'm having trouble doing it all without using global variables though, so I have another post in this group about local variable usage and how to pass it, but just when I thought I'd got it, my program has a segmentation fault. I'm not exactly sure...
27
3375
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! #include <stdlib.h> #include <stdio.h> typedef struct _node_t {
1
407
by: andrew browning | last post by:
can someone tell me what a segmentation fault is???? does it mean i am dividing by 0 somewhere?
7
5883
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our testcases we experiance Segmentation fault from the python libraries. If i know how to handle the exception for Segmentation fault , it will help me complete the run on any testcase , even if i experiance Seg Fault due to any one or many functions in...
3
5188
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection from 10g release2 PHP is configured with the following parameters './configure' '--prefix=/opt/oracle/php' '--with-apxs=/opt/oracle/apache/bin/apxs' '--with-config-file-path=/opt/oracle/apache/conf' '--enable-safe-mode' '--enable-session'...
11
5054
by: John Williams | last post by:
I've written a simple program to do XOR encryption as my first foray into understanding how encryption works. The code compiles fine, however it segmentation faults on every run. using gdb to debug it let me narrow the problem down to the Cipher function I think it faults at line 84 or 85. The program makes it's first read/cipher/write pass without issue but the second pass kills it. Using gdb to print the variables left showed me the...
7
2377
by: pooh1119 | last post by:
hi. my code below makes use of NTL(a library for doin number theory available at www.shoup.net); im trying to print the double base number representation of any large number(32 bits and/or more) but my program gives me a segmentation fault. i was unable to find out where though. could some please tell me where the fault is and how to correct it? Thanks! #include <NTL/ZZ.h> #include <NTL/vec_ZZ.h> #include <cstdlib> NTL_CLIENT
0
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10107
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.