I had declared a Boolean variable bool abc;
in a class and thought that it would be false by default. An if condition in my program, if (abc)
, turned out to be true, so I output the value of abc, and saw that it contained the value 55. Is this normal?
Do we always have to assign 'bool abc=false' to be sure that it is false?
Boolean values A variable of the primitive data type boolean can have two values: true and false (Boolean literals).
Remarks. Use the Boolean Data Type (Visual Basic) to contain two-state values such as true/false, yes/no, or on/off. The default value of Boolean is False . Boolean values are not stored as numbers, and the stored values are not intended to be equivalent to numbers.
Boolean values and operations There are just two values of type bool: true and false. They are used as the values of expressions that have yes-or-no answers. C++ is different from Java in that type bool is actually equivalent to type int. Constant true is 1 and constant false is 0.
The default value of any Object , such as Boolean , is null . The default value for a boolean is false.
Talking about primitive built-in data types (bool, char, wchar_t, short, int, long, float, double, long double), according to C++ standard, only global variables get a default value of zero if they are not explicitly initialized.
For local variables it's not required for the complier to clean up the content of the memory they are assigned to. A local variable -- if not explicitly initialized -- will contain an arbitrary value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With