[英]Why does this C program print output “10” irrespective of the for loop?
#include <stdio.h>
int main()
{
int i=10;
for (int i=1;i<=20;i++)
i++;
printf("%d",i);
return 0;
}
I don't understand that why this C program always only prints the output as 10 irrespective of the condition that has been given in the for loop. 我不明白,为什么此C程序总是只将输出打印为10,而不管在for循环中给出的条件如何。 I tried to execute it by making some changes in the for loop like
for (int i=1;i<=50;i++)
, but it returned the same output! 我试图通过在for循环中进行一些更改来执行它,例如
for (int i=1;i<=50;i++)
,但是它返回了相同的输出! What is the mistake here? 这是什么错误?
This is what your code actually says, with some comments to illustrate 这就是您的代码实际所说的,并带有一些注释来说明
#include <stdio.h>
int main()
{
int i=10; // Variable i is declared (=10)
for (int i=1;i<=20;i++) // DIFFERENT variable i is declared, hiding the first variable (=1).
{
i++; // The inner-i variable increments: 3, 5, 7, .... 19
} // inner-i is complete, and disappears. It is no longer active.
printf("%d",i); // The original Variable i is printed.
// It still has value 10, as originally initialized.
return 0;
}
It is a for
loop, so it increments according to what you write in its header : i++
here. 这是一个
for
循环,因此它会根据您在其标题中写的内容递增: i++
在这里。 You don't need to increment i
manually in the loop body by doing i++
once more. 您不需要通过再次执行
i++
在循环主体中手动递增i
。
Further than that, if you declare a variable i
before the loop, and write for (int i=1;i<=20;i++)
, it declares another variable only for the scope of the loop. 除此之外,如果在循环之前声明变量
i
并写for (int i=1;i<=20;i++)
,则它仅在循环范围内声明另一个变量。 Since you didn't put any brackets around your loop code, the latter is only made of the line i++
. 由于您没有在循环代码周围放置任何括号,因此循环代码仅由
i++
行组成。 Then, the for
scope ends, and you print the "other" i
variable, which you declared earlier, and initialized at 10. 然后,
for
范围结束,并打印您先前声明的“ other” i
变量,并将其初始化为10。
printf("%d",i);
is not part of the for
loop, only i++
is. 不是
for
循环的一部分,只有i++
是。 Indentation is not significant in C, you need to use blocks to group operations together. 缩进在C语言中并不重要,您需要使用块将操作分组在一起。 Eg
例如
for (int i=1;i<=20;i++)
{
i++;
printf("%d",i);
}
In C indentation is not part of the block-structure (like it is in Python). 在C语言中,缩进不是块结构的一部分(就像在Python中一样)。
That means your code is really something like this: 这意味着您的代码实际上是这样的:
#include <stdio.h>
int main()
{
int i=10;
for (int i=1;i<=20;i++)
i++; // In loop
printf("%d",i); // Not in loop
return 0;
}
Blocks of code have to be delimited with curly-braces {
and }
: 必须使用大括号
{
和}
分隔代码块:
#include <stdio.h>
int main()
{
int i=10;
for (int i=1;i<=20;i++)
{
i++; // In loop
printf("%d",i); // Also in loop
}
return 0;
}
C doesn't care about indentation (it only cares about whitespace to the extent that it separates tokens). C不在乎缩进(它只在乎分隔令牌的程度在乎空格)。 If you want multiple statements to be part of a
for
loop (or while
loop, or if
statement, etc.) then you need delimit those statements with curly braces: 如果要使多个语句成为
for
循环(或while
循环或if
语句等)的一部分,则需要用花括号分隔这些语句:
for ( i = 0; i < 10; i++ )
printf( "this statement is part of the loop\n" );
printf( "this statement is NOT part of the loop\n" );
vs. 与
for ( i = 0; i < 10; i++ )
{
printf( "this statement is part of the loop\n" );
printf( "so is this\n" );
}
It's generally a good idea to use curly braces even if you only have a single statement: 即使只有一条语句,使用花括号通常也是一个好主意:
while ( some_condition )
{
do_something();
}
You've also created two separate variables named i
: 您还创建了两个名为
i
单独变量:
int i = 10; // first instance
for ( int i = 1; i <= 20; i++ ) // second instance of i "shadows" the first
This works because the for
loop creates a new scope , and you can have variables in different scopes with the same name. 之所以可行,是因为
for
循环创建了一个新范围 ,并且您可以在具有相同名称的不同范围中使用变量。 The variable i
in the for
loop hides or "shadows" the variable i
declared at the beginning of the program. for
循环中的变量i
隐藏或“阴影”了在程序开头声明的变量i
。 When the for
loop exits, the i
declared within it ceases to exist, so you're printing the value of the i
declared at the beginning of the program. 当
for
循环退出时,其中声明的i
不再存在,因此您要在程序开始时打印声明的i
的值。
Indentation is not a necessary part to define a block of code in C. 缩进不是在C中定义代码块的必要部分。
According to this code, the 'i' that you increment is a different variable than what you want it to be. 根据此代码,您递增的“ i”是与您想要的变量不同的变量。
The equivalent code is: 等效代码为:
int main()
{
int n=10;
for(int i=1; i<=20;i++)
i++;
printf("%d",n);
}
But the required code is: 但是所需的代码是:
int main()
{
int i=10;
for(int i=1; i<=20;i++)
{
i++;
printf("%d",i);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.