简体   繁体   English

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

[英]What is the default value for not initialized variables in C++?

Below, you see a very very simple program : 在下面,您会看到一个非常非常简单的程序:

#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;
}

The question is , why in the output the value of c is different from the value of r and d ? 问题是,为什么在输出中c的值不同于rd的值?

在此处输入图片说明

When I change the order of them in printing on the screen, the value change! 当我在屏幕上打印时更改它们的顺序时,值会改变!

#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;
}

在此处输入图片说明

Why this time the value of r is different from the others? 为什么这次r的值与其他值不同? I just change the order of printing them on the screen! 我只是改变了在屏幕上打印它们的顺序!

What is the default value for not initialized variables in C++ C ++中未初始化变量的默认值是多少

The default value is indeterminate for local variables of fundamental types. 对于基本类型的局部变量,默认值是不确定的。

The question is , why in the output the value of c is different from the value of r and d? 问题是,为什么在输出中c的值不同于r和d的值?

Because reading uninitialized variable has undefined behaviour. 因为读取未初始化的变量具有未定义的行为。

When I change the order of variables in cout line, the value changes! 当我更改cout行中变量的顺序时,值会改变!

If you're surprised by this, then you've not understood yet what undefined behaviour or unspecified value means. 如果对此感到惊讶,那么您还不了解未定义行为或未指定值的含义。

The value of your uninitialized values can be anything and any change to your code, the change of compiler, the change of processor even the altitude of moon* may change the garbage. 未初始化值的值可以是任何值,并且对代码的任何更改,编译器的更改,处理器的更改,甚至月亮的高度*都可能更改垃圾。 Or it might not. 否则可能不会。 In this case it did. 在这种情况下,它做到了。

* not very likely a feature in most c++ compilers *在大多数C ++编译器中不太可能是一项功能

Values of uninitialized variables declared in a function is undefined. 在函数中声明的未初始化变量的值是不确定的。

Global variables will be set to 0 (or equivalent if it's not a number). 全局变量将设置为0(如果不是数字则为等效变量)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual C ++ 2005-本地int和double变量是否默认初始化为0? - Visual C++ 2005 - are local int and double variables initialized to 0 by default or not? 为什么C和C ++编译器将显式初始化和默认初始化全局变量放在不同的段中? - Why do C and C++ compilers place explicitly initialized and default initialized global variables in different segments? 外部变量是否初始化为其默认值? - are extern variables initialized to their default value? 在 C++ 中默认初始化的原始类型是什么? - What are primitive types default-initialized to in C++? 什么是在C ++中默认初始化的全局强类型枚举? - What are global strongly-typed enums initialized to by default in C++? 在构造函数中初始化的变量未保持初始化状态(C ++) - Variables initialized in constructor not staying initialized (c++) 现代C ++是否具有默认的初始化值 - Does modern c++ will have default initialized value C ++-局部未初始化变量的值 - C++ - Value of local non-initialized variables 什么是初始化为 C++ 的结构 - What are structs initialized as C++ 为什么零初始化不是c ++中非初始化变量的默认值? 是否有强制它的编译器选项? - Why is zero-initialization not the default for non-initialized variables in c++? Is there a compiler option to force it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM