C ++中的统一变量 [英] Unitialized Variables in C++

查看:63
本文介绍了C ++中的统一变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向我提出的问题是:

在C ++中,未初始化变量的值是什么?我们必须初始化所有变量吗?初始化变量的规则是什么?

What will the value of uninitialized variables be in C++? Do we have to initialize all variables? What would be the rule for initializing variables?

我已经查看了我的文字以及我手边的其他文字,但似乎找不到答案.这是我尝试过的:

I've looked in my text as well as another text that I have on hand and can't seem to find the answer. Here's what I've attempted:

C ++中未初始化变量的值取决于未初始化变量分配给内存中存储的先前值.不需要初始化所有变量,例如如果将通过用户输入立即为变量分配值,则无需初始化变量.良好的编程习惯应该是初始化变量,如果变量未初始化,则注释应说明将变量保留为未初始化的原因.

The value of uninitialized variables in C++ depends on the previous value stored in the memory that the uninitialized variable is assigned to. Initializing all variables is not a requirement, e.g. a variable does not need to be initialized if the variable will immediately be assigned a value via user input. Good programming practice should be to initialize a variable and if a variable is uninitialized, then comments should explain the reasoning behind leaving the variable uninitialized.

我错过了什么吗?有谁可以向我指出他们的资源吗?我错过了变量初始化的规则"吗?谢谢.

Am I missing something? Is their a resource anyone could point me to? Is there a "rule" to variable initialization that I missed? Thank you.

推荐答案

通常,您不知道未初始化变量中存储了什么,因此初始化变量始终是一个好主意.这样一来,您就可以避免以后可能出现的混乱.例如,如果您稍后在程序中打印出变量进行调试(并且尚未初始化),则可能会打印出一些奇怪的值.

Generally, you have no idea what is stored in an uninitialized variable so it is always a good idea to initialize variables. This way you can avoid possible confusion later on; for example, if you went to print out a variable later on in your program for debugging (and you hadn't initialized it yet) it would likely print out some strange value.

如果您需要更多信息,似乎您的问题也在此处得到了解答: http://www.cplusplus.com/forum/general/62807/

If you want some more info, it looks like your question was also answered here: http://www.cplusplus.com/forum/general/62807/

这篇关于C ++中的统一变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆