为什么带有逻辑与运算符和递增运算符的 C++ 程序给出的输出与预期的不同?

Why c++ program with logical and operator and increment operator gives different output than expected?

我正在尝试遵循 C 代码。我认为这段代码的输出应该是 0 0 0 0。但是在执行它之后输出是 0 -1 -1 0。任何人都可以解释输出是如何产生的。

    #include<iostream>
    using namespace std;
    int main()
    {
        int x=-1, y=-1, z=-1;
        int w= ++x && ++y && ++z;
        cout<<x<<" "<<y<<" "<<z<<" "<<w<<endl;
        return 0;
    }

这是因为 && 运算符的短路行为。 ++x 评估为 0,这将被视为 false。因此,只有 ++x 会被评估,其余表达式不会被评估,表达式 ++x && ++y && ++z 的最终值将是 0 并将分配给 w.