请解释这个C代码的for循环和输出
explain the for loop and output of this C code please
谁能给我解释一下这段 C 代码。这是我在考试中为查找输出而提出的问题之一。
#include<stdio.h>
int r()
{
static int num = 7;
return num--;
}
int main()
{
for(r(); r(); r())
printf("%d\n",r() );
}
我不明白 for 循环是如何工作的(条件和 increment/decrement 语句)以及这段代码如何给出这个输出。
Output:
5
2
让我们考虑一下循环
for(r(); r(); r())
函数r
在init表达式中被调用
for(r(); r(); r())
^^^
它的静态变量num
被减少等于6
。
然后检查循环的条件
for(r(); r(); r())
^^^
再次调用该函数,其静态变量变为等于 5
。
内调用 printf
printf("%d\n",r() );
有调用函数r
。因为在函数中使用了 post-递减运算符,所以函数 returns 5 但是 num
的值变得等于 4
。所以输出返回值5。
然后计算循环的第三个表达式。
for(r(); r(); r())
^^^
num
等于 3.
再次检查条件,num 变为等于 2。
for(r(); r(); r())
^^^
在printf
的调用中,输出了返回值2,但在函数r
中,num的值被递减并等于1。
之后计算循环的第三个表达式。
for(r(); r(); r())
^^^
num 变为 0。
该值由函数 r 在条件中返回。在函数内,静态变量现在等于 -1。
由于返回值为 0,循环停止迭代。
来自 C 标准(6.5.2.4 后缀递增和递减运算符)
2 The result of the postfix ++ operator is the value of the operand.
As a side effect, the value of the operand object is incremented (that
is, the value 1 of the appropriate type is added to it)...
和
3 The postfix -- operator is analogous to the postfix ++ operator,
except that the value of the operand is decremented (that is, the
value 1 of the appropriate type is subtracted from it).
for
形式的循环
for(init; condition; repeat) {
body;
}
大致相当于
init;
while (condition) {
body;
repeat;
}
所以你给出的语句等同于:
r();
while (r()) {
printf("%d\n", r());
r();
}
第一次调用 r
将静态变量 num
初始化为 7
。然后它 returns 这个值,同时也将它递减到 6
。此 return 值未使用。
然后测试while
条件。此调用 r()
return 的值 6
,同时将 num
递减为 5
。由于这是非零,条件成功,所以我们进入循环体。
然后执行printf("%d\n", r());
。此调用 r()
return 的值 5
,同时将变量递减为 4
。 return 值由 printf()
打印,所以我们看到 5
.
然后我们称r()
为重复操作。这会将 num
递减为 3
,并忽略 return 值。
然后我们回到while
条件。这会将 num
递减为 2
,同时 return 会 3
。这是非零的,所以我们进入正文。
然后我们执行printf("%d\n", r());
。与上一次迭代一样,r()
return 是 num
的当前值,同时将其递减为 1
,因此打印 2
。
然后我们称r()
为重复操作。这会将 num()
递减为 0
和 returns 1
,但忽略 return 值。
我们回到while
条件。这一次,r()
return 将当前值 0
递减为 -1
。这次,return 值为零,因此循环停止。
谁能给我解释一下这段 C 代码。这是我在考试中为查找输出而提出的问题之一。
#include<stdio.h>
int r()
{
static int num = 7;
return num--;
}
int main()
{
for(r(); r(); r())
printf("%d\n",r() );
}
我不明白 for 循环是如何工作的(条件和 increment/decrement 语句)以及这段代码如何给出这个输出。
Output:
5
2
让我们考虑一下循环
for(r(); r(); r())
函数r
在init表达式中被调用
for(r(); r(); r())
^^^
它的静态变量num
被减少等于6
。
然后检查循环的条件
for(r(); r(); r())
^^^
再次调用该函数,其静态变量变为等于 5
。
内调用 printf
printf("%d\n",r() );
有调用函数r
。因为在函数中使用了 post-递减运算符,所以函数 returns 5 但是 num
的值变得等于 4
。所以输出返回值5。
然后计算循环的第三个表达式。
for(r(); r(); r())
^^^
num
等于 3.
再次检查条件,num 变为等于 2。
for(r(); r(); r())
^^^
在printf
的调用中,输出了返回值2,但在函数r
中,num的值被递减并等于1。
之后计算循环的第三个表达式。
for(r(); r(); r())
^^^
num 变为 0。
该值由函数 r 在条件中返回。在函数内,静态变量现在等于 -1。
由于返回值为 0,循环停止迭代。
来自 C 标准(6.5.2.4 后缀递增和递减运算符)
2 The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it)...
和
3 The postfix -- operator is analogous to the postfix ++ operator, except that the value of the operand is decremented (that is, the value 1 of the appropriate type is subtracted from it).
for
形式的循环
for(init; condition; repeat) {
body;
}
大致相当于
init;
while (condition) {
body;
repeat;
}
所以你给出的语句等同于:
r();
while (r()) {
printf("%d\n", r());
r();
}
第一次调用 r
将静态变量 num
初始化为 7
。然后它 returns 这个值,同时也将它递减到 6
。此 return 值未使用。
然后测试while
条件。此调用 r()
return 的值 6
,同时将 num
递减为 5
。由于这是非零,条件成功,所以我们进入循环体。
然后执行printf("%d\n", r());
。此调用 r()
return 的值 5
,同时将变量递减为 4
。 return 值由 printf()
打印,所以我们看到 5
.
然后我们称r()
为重复操作。这会将 num
递减为 3
,并忽略 return 值。
然后我们回到while
条件。这会将 num
递减为 2
,同时 return 会 3
。这是非零的,所以我们进入正文。
然后我们执行printf("%d\n", r());
。与上一次迭代一样,r()
return 是 num
的当前值,同时将其递减为 1
,因此打印 2
。
然后我们称r()
为重复操作。这会将 num()
递减为 0
和 returns 1
,但忽略 return 值。
我们回到while
条件。这一次,r()
return 将当前值 0
递减为 -1
。这次,return 值为零,因此循环停止。