0

Background

In C++, booleans are stored as integers:

Boolean values are not actually stored in Boolean variables as the words “true” or “false”. Instead, they are stored as integers: true becomes the integer 1, and false becomes the integer 0. Similarly, when Boolean values are evaluated, they don’t actually evaluate to “true” or “false”. They evaluate to the integers 0 (false) or 1 (true). Because Booleans actually store integers, they are considered an integral type.

However, this seemed to contradict something I have seen. I recently came across a branched file that seemed to imply that the float value 0.0 is also equivalent to false as shown below:

Base Version

arraySet_[m][n].resize(VALUES().maximumCellsPerRow[r], 0.0);

Branched Version

arraySet_[m][n].resize(VALUES().maximumCellsPerRow[r],false);

Given this equivalence I have seen, I am now wondering...

Question

Even though booleans are stored as integers, does C++ interpret the float value of 0.0 as false as well?

isakbob
  • 1,439
  • 2
  • 17
  • 39
  • 1
    `0` is `false`. Anything else is `true`. – Jesper Juhl Oct 09 '19 at 19:02
  • 2
    imho it is not really helpful to think of booleans as being stored as integers. Thats rather an implementation detail. Boolean values are `true` and `false` and `0` converts to `false`, thats all you need to remember – 463035818_is_not_an_ai Oct 09 '19 at 19:05
  • 1
    That tutorial is misleading at best, and wrong at worst. [How bools are _stored_ is not known except to your compiler](https://stackoverflow.com/a/56369692/560648), and those claims about how bools are _evaluated_ internally are too vacuous to even criticize. Please stop learning from online tutorials written by randomers and pick up [a good peer-reviewed book](https://stackoverflow.com/q/388242/560648) instead. – Lightness Races in Orbit Oct 09 '19 at 19:21
  • 1
    Anyway, that dupe answers your question. – Lightness Races in Orbit Oct 09 '19 at 19:23
  • Its a _value_ thing, not a _type_ issue. – chux - Reinstate Monica Oct 09 '19 at 22:44

0 Answers0