使用srand函数进行分段错误 [英] Segmentation Fault using the srand function

查看:154
本文介绍了使用srand函数进行分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在cygwin平台上使用gcc编译器,在C语言编程方面,我是一个初学者,想知道为什么随时可以使用

我跑带有以下代码的.exe我得到一个分段错误

(核心转储)错误:


#include< stdio.h>


main()


{


int x,iNumQuestions,iResponse,iRndNum1,iRndNum2;

srand(time());


printf(\ n输入要问的问题数量:);

scanf("%d",& iNumQuestions);


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

iRndNum1 = rand()%10 + 1;

iRndNum2 = rand()%10 + 1;


printf(\ n什么是%dx%d:,iRndNum1,iRndNum2);

scanf("%d",& iResponse);


if(iResponse == iRndNum1 * iRndNum2)

printf(" \ nCorrect!\ n");


else

printf(\ n正确答案是%d \ n,iRndNum1 * iRndNum2);


}


}

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写于06/27/06 13:14,:


jtagpgmr wrote On 06/27/06 13:14,:
我是目前在cygwin平台上使用gcc编译器,我是一个初学者,当谈到用C编程并想知道为什么随时
我用以下代码运行.exe我得到了一个细分故障
(核心倾销)错误:

#include< stdio.h>

main()


int x,iNumQuestions ,iResponse,iRndNum1,iRndNum2;
srand(time());
[...]
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());
[...]




这是你的近因问题:时间()

函数需要一个参数,但你没有提供一个。


不要以为这是唯一的错误你的

代码。要了解其他人,请在诊断中要求gcc更详细

:添加-W -Wall到另一个命令行

标志,并注意编译器然后告诉你的内容。

(如果你真的很勇敢,请使用&-W- Wall -ansi -pedantic或

" -W -Wall -std = c99 -pedantic。这将是一次学习

的体验。)


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



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


以下是什么意思?

函数srand的隐式声明

函数时间的隐式声明

函数rand隐式声明


Eric Sosman写道:
What does the following mean?
implicit declaration of function srand
implicit declaration of function time
implicit declaration of function rand

Eric Sosman wrote:
jtagpgmr写于06/27/06 13:14,:
jtagpgmr wrote On 06/27/06 13:14,:
我目前正在cygwin平台上使用gcc编译器,在C语言编程时我是初学者,想知道为什么随时可以使用
我使用以下代码运行.exe,我得到一个分段错误
(核心转储)。错误:

#include< stdio.h>

main()


int x,iNumQuestions ,iResponse,iRndNum1,iRndNum2;
srand(time());
[...]
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());
[...]



这是您问题的直接原因:时间()
函数需要参数,但你没有提供参数。

不要以为这是你的代码中唯一的错误。要了解其他人,请在诊断中要求gcc更详细:添加-W -Wall到其他命令行
标志,并注意编译器然后告诉你的内容。
(如果你真的很勇敢,请使用-W -Wall -ansi -pedantic或
" -W -Wall -std = c99 -pedantic"。这将是一种学习的经验。)

-
呃********* @ sun.com






jtagpgmr说:
jtagpgmr said:
以下是什么意思?
隐式声明函数srand


它意味着你忘了#include< stdlib.h>

函数时间的隐式声明


这意味着你忘了#include< time.h>

隐式声明函数rand
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




这意味着你忘了#include< stdlib.h>


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)



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)


这篇关于使用srand函数进行分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆