c++ - 如果不为每个分配 1, "int x,y,z = 1 "在做什么?

标签 c++ variable-assignment

我是 C/C++ 的新手,我注意到我的程序计算了错误的值。我发现问题出在我对变量声明在 C/C++ 中的工作方式的理解上。 x,y,z = 1,2,3在 Python 中工作正常,其中 x,y,z = 1才不是。 int x,y,z = 1,2,3在 C/C++ 中不起作用,但 int x,y,z = 1确实,有点,因为cout << x;输出 16,我预计它是 1。

#include <iostream>
using namespace std;

int main() {

    int x, y, z = 1;
    cout << x;
    
    return 0;

}

// The output is 16

操作是什么int x,y,z = 1做,如果不给每个分配 1?

最佳答案

它是以下内容的简写:

int x;
int y;
int z = 1;

xy 未分配。

关于c++ - 如果不为每个分配 1, "int x,y,z = 1 "在做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68900397/

相关文章:

c++ - tbb::parallel_for_each取消时未执行

c++ - 与 C++ 函数中的参数混淆?

c++ - 静态库中的"Conditional jump or move depends on uninitialised value"

return - 函数输出一个列表但返回 nil

java - 对变量 [Something] 的赋值无效

带返回的 C++ 析构函数

c++ - 将 std::vector 分配给 std::valarray

python - 如何修复签名中 Tensorflow 1.14.0rc 中的 'strided slice assignment are not compatible with expected types'?

java - 在Java中,当我在线程扩展类的run()函数中时,我无法为变量赋值

c++ - 如何从类外修改 vector