【发布时间】:2018-12-28 15:43:48
【问题描述】:
我正在尝试在 for 循环中增加变量 x 并在每次迭代时打印它。
include <stdio.h>
int x = 0;
void main()
{
int c;
for (c = 1; c <= 5; c++)
x++;
printf("%d", x);
}
我想要的输出是:
12345
但是这段代码只打印出来:
5
当我不增加 x 时,我可以在每次迭代时打印:
# include <stdio.h>
int x = 0;
void main()
{
int c;
for (c = 1; c <= 5; c++)
//x++;
printf("%d", x);
}
输出:
00000
为什么在循环中增加 x 会改变 printf 的行为?
【问题讨论】:
-
您的代码块周围缺少花括号。
-
您可能有 Python 背景。 :)