- 1 year ago
BOOL returning -1, 0 or positive
In C/C++, the BOOL
type is typically defined as an integer type that can take one of three possible values: -1
, 0
, or a positive value. The standard convention is to use FALSE
for 0
and TRUE
for non-zero values. However, the specific values used for FALSE
and TRUE
may vary depending on the library or framework being used.
Here's an example illustrating the usage of BOOL
in C/C++:
cpp#include <iostream>
// Example function that returns a BOOL
BOOL isEven(int num) {
if (num % 2 == 0) {
return TRUE; // Non-zero value indicates true
} else {
return FALSE; // Zero value indicates false
}
}
int main() {
int num = 5;
BOOL result = isEven(num);
if (result == TRUE) {
std::cout << num << " is even." << std::endl;
} else {
std::cout << num << " is odd." << std::endl;
}
return 0;
}
In this example, the isEven
function returns a BOOL
value indicating whether a given number is even (TRUE
) or odd (FALSE
). The main function then checks the value of the result
variable to determine whether the number is even or odd.
It's worth noting that BOOL
is commonly defined as typedef int BOOL;
in Windows programming, where FALSE
is 0
and TRUE
is 1
(although other non-zero values may also be treated as TRUE
in practice). However, in other libraries or frameworks, the definition and specific values for BOOL
may vary. Always refer to the documentation or headers of the specific library or framework you are using to determine the exact behavior of BOOL
in that context.