Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aren't Boolean variables always false by default?

Tags:

c++

syntax

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?

like image 288
Nav Avatar asked Jan 07 '11 03:01

Nav


People also ask

Can boolean be false?

Boolean values A variable of the primitive data type boolean can have two values: true and false (Boolean literals).

Is a Boolean variable true by default?

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.

Is Boolean value true or false?

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.

Is boolean false by default?

The default value of any Object , such as Boolean , is null . The default value for a boolean is false.


1 Answers

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.

like image 119
Nylon Smile Avatar answered Nov 16 '22 03:11

Nylon Smile