What is the boolean value of integers other than 0 or 1?
I wrote a simple function to get a new filename based on a set of files to be named cam1_0.bmp, cam1_1.bmp and tried that.
static int suffix = 0;
std::string fPre("cam");
std::ifstream fs;
std::string fName;
do {
fName = fPre;
fName.append(std::to_string(camera)).append("_").append(std::to_string(suffix)).append(".bmp");
fs.open(fName);
} while (fs.good() && ++suffix);
This works and it made me wonder what is the standard, defined behavior of the corresponding boolean values for numeric values other than 0 or 1. In this experiment, I know that all values, including negative values other than 0, are true ... Only 0 is considered false according to the standard?
source to share
In C ++, integers have no boolean values. (Different languages have different rules, but this question is about C ++.)
The result of converting an integer value to a type bool
(a conversion that is often done implicitly) is well defined. The conversion 0
to bool
is false
; the result of converting any non-zero value to bool
is true
.
The same applies to floating point values ( 0.0
converted to false
, all other values converted to true
) and pointers (null pointer converted to false
, all non-null pointer converted to true
).
source to share
Yes, any number other than 0 is considered true for booleans.
Visit http://www.vbforums.com/showthread.php?405047-Classic-VB-Why-is-TRUE-equal-to-1-and-not-1 for an explanation.
source to share