C:帮我理解输出

0 c output

任何人都可以帮我理解这段代码的输出吗?

#include <stdio.h>

int main()
{
    int x = 1, y = 1;

    for(; y; printf("%d %d \n", x, y))
    {
        y = x++ <= 5;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是:

2 1
3 1
4 1
5 1
6 1
7 0

Som*_*ude 5

一个for循环的形式

for (a; b; c)
{
    d;
}
Run Code Online (Sandbox Code Playgroud)

相当于

{
    a;

    while (b)
    {
        d;

        c;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我们采取你的循环

for(; y; printf("%d %d \n", x, y))
{
    y = x++ <= 5;
}
Run Code Online (Sandbox Code Playgroud)

它相当于

{
    // Nothing

    // Loop while y is non-zero
    while (y)
    {
        // Check if x is less than or equal to 5, assign that result to y
        // Then increase x by one
        y = x++ <= 5;

        printf("%d %d \n", x, y);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在应该更容易理解正在发生的事情.

另外:请记住,对于布尔结果(就像您从比较中得到的结果),true等于1,而false等于0.