Is it safe to compare boolean variable with 1 and 0 in C, C++? [duplicate]
问题 This question already has answers here : Can I assume (bool)true == (int)1 for any C++ compiler? (4 answers) Closed 6 years ago . Consider the code bool f() { return 42; } if (f() == 1) printf("hello"); Does C (C99+ with stdbool.h) and C++ standards guarantee that "hello" will printed? Does bool a = x; is always equivalent to bool a = x ? 1 : 0; 回答1: In C++, bool is a built-in type. Conversions from any type to bool always yield false (0) or true (1). Prior to the 1999 ISO C standard, C did