#include<stdio.h>
int main(int argc,char**argv)
{
int a=2;
int b=1;
bool c=a*b;
printf("Bool c:%d\n",c);
return 0;
}在 C++ 中,将 int 类型转换为 bool 类型遵循以下规律:
非零值:任何非零的 int 值都会转换为 true。
零值:int 值为零时会转换为 false。
int main() { int a = 0; int b = 42; bool boolA = static_cast<bool>(a); // 转换为 false
bool boolB = static_cast<bool>(b); // 转换为 true
std::cout << "boolA: " << boolA << std::endl; // 输出 0 (false)
std::cout << "boolB: " << boolB << std::endl; // 输出 1 (true)
return 0;
}