简体   繁体   English

为什么我的程序为什么在向数组赋值的语句中退出循环?

[英]Why does my program exit the loop at a statement where I'm assigning a value to an array?

I wrote a simple program to print the Fibonacci series up to a certain value, store it in an array and calculate it's sum. 我编写了一个简单的程序,将斐波那契数列打印到一定值,将其存储在数组中并计算总和。

#include<stdio.h>
int main()
{
    int i,a=0,b=1,c,limit,sum=1;
    int fib[limit];
    printf("Enter the number of terms: \n");
    scanf("%d", &limit);
    fib[0]=0;
    fib[1]=1;
    printf("0 \n");
    printf("1 \n");
    for(i=2; i<limit; i++)
    {
        c=a+b;
        printf("%d \n",c);
        fib[i]=c;
        a=b;
        b=c;

    }
    for(i=0; i<limit; i++)
    {
        sum+=fib[i];
    }
    printf("The sum is %d", sum);

    }

But it doesn't output the series until the limit and I believe this line may be the cause. 但是直到极限值时它才输出序列,我相信这可能是原因。

fib[i]=c;

I'm not sure why though. 我不确定为什么。 Could you please help me? 请你帮助我好吗?

Thank You 谢谢

int fib[limit];
printf("Enter the number of terms: \n");
scanf("%d", &limit);

will be 将会

printf("Enter the number of terms: \n");
scanf("%d", &limit);
int fib[limit];

Otherwise you were declaring the VLA with a variable having indeterminate content. 否则,您将使用具有不确定内容的变量声明VLA。 This is undefined behavior. 这是未定义的行为。 In your case program crashed when you tried to use it. 以您为例,当您尝试使用程序时,该程序崩溃了。

In case you are in C89 then you need to initialize the size with some maximum value as VLA is not supported in C89. 如果您使用的是C89则需要使用一些最大值来初始化大小,因为C89不支持VLA。

void MAXLIMIT 1000
...

int fib[MAXLIMIT];
int i,a=0,b=1,c,limit,sum=1;
int fib[limit]; printf("Enter the number of terms: \n");
scanf("%d", &limit);

limit isn't initialized when you declare the variable length array fib . 声明可变长度数组fib时未初始化limit limit could be any value at all. limit可以是任何值。 This means that the length is unknown, which will surely cause problems. 这意味着长度未知,这肯定会引起问题。 It's undefined behavior. 这是未定义的行为。

Also, I noticed that you tagged this with C89. 另外,我注意到您用C89标记了它。 In the 1989 standard variable length arrays are not allowed. 在1989年,不允许使用标准的可变长度数组。 Any standards compliant compiler will refuse to compile this, or at the very least emit a stern warning. 任何符合标准的编译器都将拒绝对此进行编译,或者至少会发出严厉的警告。 VLAs are permitted in the C99 standard though. 但是,C99标准允许使用VLA。

int i,a=0,b=1,c,limit,sum=1;
printf("Enter the number of terms: \n");
scanf("%d", &limit);
int fib[limit];

If you did it like this, it would work fine, and be perfectly valid C99. 如果您这样做的话,它将可以正常工作,并且是完全有效的C99。 C89 still wouldn't work because of the VLA and the fact that fib isn't defined before printf and scanf . 由于VLA以及在printfscanf之前未定义fib的事实,C89仍然无法工作。

There are some mistakes in your code . 您的代码中存在一些错误。

The limit value contain the garbage value at this line 限制值包含此行的垃圾值

.....
int i,a=0,b=1,c,limit,sum=1;
....

& then you initialize an array with the size of garbage value . &然后使用垃圾值的大小初始化一个数组。

....
int fib[limit];
....

after that you take the 'limit' value as the size of array. 之后,您将'limit'值作为数组的大小。

....
printf("Enter the number of terms: \n");
scanf("%d", &limit);
....

but you can initilaize the array before taking the limit value . 但是您可以在获取极限值之前初始化数组。

so the correct code is , 所以正确的代码

    ....
    printf("Enter the number of terms: \n");
    scanf("%d", &limit);
    int fib[limit];
    ....

You have initialze the array after taking the size of array as named 'limit' 在将数组的大小命名为“ limit”之后,您已经初始化了数组

Now you code will work perfectly .& sum of Fibonacci series correctly evalutaed. 现在您的代码将可以完美地工作。正确评估了斐波那契数列之和。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么即使满足退出条件,我的C程序也不会跳出while循环 - Why does my C program do not jump out the while loop even when the exit condition is met 为什么即使我在 3 个大小的数组上分配了 4 个值,我的代码仍在运行? - Why is my code running even though I'm assigning 4 values on a 3 sized array? 为int指针分配值时,为什么我的C程序崩溃? - Why does my C program crash when assigning a value to an int pointer? 什么是退出值 5,为什么我在运行 C 程序时不断收到此错误? - What is exit value 5 and why am I keep getting this error while running my C program? return 0 是退出程序还是退出循环? - does return 0 exit a program or exit a loop? 为什么Sublime Text报告我的程序以退出代码1终止? - Why does Sublime Text report that my program terminates with exit code 1? 为什么我的程序在输入后退出 - Why does my program exit after taking a input 为什么我的if语句会创建一个无限循环? - Why does my if statement create an infinite loop? 我正在尝试构建2 * 2数组,但是我的程序要求输入5个信息。 为什么? - I m trying to build a 2*2 array but my program asks for 5 inputs. Why? 为什么即使有“if”语句,我的程序也不检查位域成员的值? - Why does my program not check the value of a bitfield member even though there is an “if” statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM