Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C+;中未初始化变量的默认值是什么+;?_C++ - Fatal编程技术网

C++ C+;中未初始化变量的默认值是什么+;?

C++ C+;中未初始化变量的默认值是什么+;?,c++,C++,下面是一个非常简单的程序: #include <stdlib.h> #include <iostream> using namespace std; int main() { int r; int c; int d; cout<<"r="<<r<<endl<<"c="<<c<<endl<<"d="<<d; return 0; }

下面是一个非常简单的程序:

#include <stdlib.h>
#include <iostream>

using namespace std;

int main()
{
    int r;
    int c;
    int d;

    cout<<"r="<<r<<endl<<"c="<<c<<endl<<"d="<<d;
    return 0;
}

为什么这次
r
的值与其他值不同?我只是改变了在屏幕上打印它们的顺序

C中未初始化变量的默认值是什么++

对于基本类型的局部变量,默认值是不确定的

问题是,为什么在输出中c的值与r和d的值不同

因为读取未初始化的变量具有未定义的行为

当我改变cout行中变量的顺序时,值就改变了

如果您对此感到惊讶,那么您还不明白未定义的行为或未指定的值意味着什么

未初始化值的值可以是任何值,对代码的任何更改、编译器的更改、处理器的更改甚至月球的高度*都可能会更改垃圾。也可能不是。在这种情况下,它做到了


<> P>>大多数C++编译器中的一个特性

函数中声明的未初始化变量的值未定义。< /P>
全局变量将设置为0(如果不是数字,则设置为等效值)。

它将是内存中的任何值。未定义的行为()这就是我总是初始化auto r=int()的原因。回答你的编辑,>>>>未定义@Abraham,今天,在美国,PST日期是2015年3月27日。29号还没有发生。请你再看一看这个问题好吗?我更新了它。当我更改
cout
行中变量的顺序时,值会更改!我接受了答案,但如果您有任何其他信息,请添加。谢谢
#include <stdlib.h>
#include <iostream>

using namespace std;

int main()
{
    int r;
    int c;
    int d;

    cout<<"c="<<c<<endl<<"r="<<r<<endl<<"d="<<d;    

    return 0;
}