为什么这会创建一个无限循环

时间:2021-06-06 08:46:48

Consider the following C program:

考虑以下C程序:

int number;
int sum;
char i;
printf("Enter a number: ");
scanf("%d", &number);
while (i != 'q')
{
    printf("Enter a number: ");
    scanf("%d", &number);
    sum = number + number;
}
sum = number + number;
printf("The sum is %d", sum);    

Why does this goes into an infinite loop? I don't see any error in the code.

为什么这会进入无限循环?我没有在代码中看到任何错误。

4 个解决方案

#1


3  

i is uninitialized. When you check the condition

我没有初始化。当你检查条件

while (i != 'q')

anything can happen(Undefined Behavior) because you are checking an uninitialized variable. Moreover, you don't change the value of i in the loop. How and why do you expect the condition to become false?

任何事情都可能发生(未定义的行为),因为您正在检查未初始化的变量。而且,您不会在循环中更改i的值。您是如何以及为什么期望这种情况变得虚假?


scanf with %d will not scan chars. And your code dosen't make much sense anyway. It might make more sense if you tell us what it needs to do.

带有%d的scanf不会扫描字符。而且你的代码无论如何都没有多大意义。如果你告诉我们它需要做什么,可能会更有意义。

I think you want this:

我想你想要这个:

char number;
int sum = 0;
printf("Enter a number (enter 'q' to exit): ");
scanf("%c", &number);
while (number != 'q')
{
    sum += number-'0';
    printf("Enter a number: ");
    scanf("%c", &number);
}
printf("The sum is %d", sum);  

If you are wondering why and what -'0' does, you need to have a look at the ASCII table. Each character has its own integer value as seen in the ASCII table.

如果你想知道为什么和'-0'做什么,你需要看一下ASCII表。每个字符都有自己的整数值,如ASCII表中所示。

#2


2  

First you do not initialize char i; and leads to undefined behavior, then in your loop you never modify:

首先,你不初始化char i;并导致未定义的行为,然后在你的循环中你永远不会修改:

while (i != 'q')
{
    printf("Enter a number: ");
    scanf("%d", &number);
    sum = number + number;
}

This code makes no sense. When do you expect i to become 'q'?

这段代码毫无意义。你什么时候想我成为'q'?

#3


0  

The code is creating an infinite loop because you are using while (i!='q'). Here you haven't initialized the value of i, as result the compiler sets a default value to the char i, which is not equal to 'q'.
Hence, whenever you are checking the condition in while loop it comes out to be true. Therefore a continues loop is formed.
To improve
You can improve it by setting an input for character i in your program. Or change the condition to any thing else from your program. One solution is:--

代码正在创建一个无限循环,因为你正在使用while(i!='q')。这里你没有初始化i的值,结果编译器为char i设置了一个默认值,它不等于'q'。因此,无论何时在while循环中检查条件,它都是真的。因此形成了连续循环。要改进您可以通过在程序中设置字符i的输入来改进它。或者将条件更改为程序中的任何其他内容。一个解决方案是: -

 int number;
int sum;
char i;
printf("Enter a number: ");
scanf("%d", &number);
while (i != 'q')
{
    printf("Enter a number: ");
    scanf("%d", &number);
    sum = number + number;
    printf("Press q to terminate: ");
    scanf("%c", &i);
}
sum = number + number;
printf("The sum is %d", sum);    

#4


0  

First of all i is uninitialized and moreover inside the loop the value of i is not either initialized or changed.

首先,我是未初始化的,而且在循环内部,i的值既未初始化也未更改。

#1


3  

i is uninitialized. When you check the condition

我没有初始化。当你检查条件

while (i != 'q')

anything can happen(Undefined Behavior) because you are checking an uninitialized variable. Moreover, you don't change the value of i in the loop. How and why do you expect the condition to become false?

任何事情都可能发生(未定义的行为),因为您正在检查未初始化的变量。而且,您不会在循环中更改i的值。您是如何以及为什么期望这种情况变得虚假?


scanf with %d will not scan chars. And your code dosen't make much sense anyway. It might make more sense if you tell us what it needs to do.

带有%d的scanf不会扫描字符。而且你的代码无论如何都没有多大意义。如果你告诉我们它需要做什么,可能会更有意义。

I think you want this:

我想你想要这个:

char number;
int sum = 0;
printf("Enter a number (enter 'q' to exit): ");
scanf("%c", &number);
while (number != 'q')
{
    sum += number-'0';
    printf("Enter a number: ");
    scanf("%c", &number);
}
printf("The sum is %d", sum);  

If you are wondering why and what -'0' does, you need to have a look at the ASCII table. Each character has its own integer value as seen in the ASCII table.

如果你想知道为什么和'-0'做什么,你需要看一下ASCII表。每个字符都有自己的整数值,如ASCII表中所示。

#2


2  

First you do not initialize char i; and leads to undefined behavior, then in your loop you never modify:

首先,你不初始化char i;并导致未定义的行为,然后在你的循环中你永远不会修改:

while (i != 'q')
{
    printf("Enter a number: ");
    scanf("%d", &number);
    sum = number + number;
}

This code makes no sense. When do you expect i to become 'q'?

这段代码毫无意义。你什么时候想我成为'q'?

#3


0  

The code is creating an infinite loop because you are using while (i!='q'). Here you haven't initialized the value of i, as result the compiler sets a default value to the char i, which is not equal to 'q'.
Hence, whenever you are checking the condition in while loop it comes out to be true. Therefore a continues loop is formed.
To improve
You can improve it by setting an input for character i in your program. Or change the condition to any thing else from your program. One solution is:--

代码正在创建一个无限循环,因为你正在使用while(i!='q')。这里你没有初始化i的值,结果编译器为char i设置了一个默认值,它不等于'q'。因此,无论何时在while循环中检查条件,它都是真的。因此形成了连续循环。要改进您可以通过在程序中设置字符i的输入来改进它。或者将条件更改为程序中的任何其他内容。一个解决方案是: -

 int number;
int sum;
char i;
printf("Enter a number: ");
scanf("%d", &number);
while (i != 'q')
{
    printf("Enter a number: ");
    scanf("%d", &number);
    sum = number + number;
    printf("Press q to terminate: ");
    scanf("%c", &i);
}
sum = number + number;
printf("The sum is %d", sum);    

#4


0  

First of all i is uninitialized and moreover inside the loop the value of i is not either initialized or changed.

首先,我是未初始化的,而且在循环内部,i的值既未初始化也未更改。