# Do I have to use BOOL?

#1

float truckWeight = 34562.8;

``````BOOL isNotLegal = ((truckWeight > 0.0) && (truckWeight < 40000.0));
if (isNotLegal) {
printf("Truck weight is not within legal range.\n");
}

return 0;
``````

What the Boolean variable (BOOL) means? Do I must use it? Or can I simply use “float” before “isNotLegal”?

#2

I’m not sure I understand. You don’t need to have a variable for the boolean value at all:

[code]float truckWeight = 34562.8;

if ((truckWeight > 0.0) && (truckWeight < 40000.0)) {
printf(“Truck weight is not within legal range.\n”);
}

return 0;[/code]

And if you really want one, you can use any sort of integer type:

[code]float truckWeight = 34562.8;

int isNotLegal = ((truckWeight > 0.0) && (truckWeight < 40000.0));
if (isNotLegal) {
printf(“Truck weight is not within legal range.\n”);
}

return 0;[/code]

#3

Renan,

If I’m understanding correctly, a BOOL variable is for storing true/false values. You could use a int instead, since in C, 0 is equal to false, and 1, 2, 3, 4, 5, etc… (even -1 I think) is equal to true. I’m not sure what would happen if you tried to use a float, though I don’t suspect it’s a very good idea. If you know the expression could only have true or false results (like x > y), then using a BOOL would make for the clearest code, and reduce the chances of introducing a logical error.

e: I’ll defer to the author – great book Aaron!

e2: Aaron, I think they are asking if you could use a float type to store Boolean results – I don’t know C well enough to know whether it would work technically, but I’m going to go ahead and say it’s not a great idea even if it does.

#4

I think I understood it now, thank you guys. I’m learning many things with this book

#5

I’m having a problem with the example given on page 24 of Objective-C Programming.
I get 2 errors when building:

1. Use of undeclared identifier ‘isNotLegal’
2. Use of undeclared identifier ‘BOOL’

Here is the code from p. 24:

#include <stdio.h>
int main(int argc, const char * argv[])
{
float truckWeight = 34563.8;
BOOL isNotLegal = !((truckWeight > 0.0) && (truckWeight <40000.0));
if (isNotLegal)
{
printf(“Truck weight is not within the legal range.\n”);
}
return 0;
}

#6

That’s my mistake:
viewtopic.php?f=136&t=3029