Problem with Boolean variables p.20


#1

Hi

I’m working through the book and I’ve stumbled upon a problem with boolean variables. When I try the following code from the book I keep getting an error that BOOL is undeclared.

[code]#include <stdio.h>

int main (int argc, const char * argv[])
{
float truckWeight = 34563.8;

// Is it under the limit?
if (truckWeight < 40000.0) {
    printf("It is a light truck\n");
} else {
    printf("It is a heavy truck\n");
}

// Is it in the legal range?
if ((truckWeight > 0.0) && (truckWeight < 40000.0)) {
    printf("Truck weight is within legal range.\n");
}

// Declare a Boolean variable to hold conditional
BOOL isNotLegal = !((truckWeight > 0.0) && (truckWeight < 40000.0));
if (isNotLegal) {
    printf("Truck weight is not within legal range.\n");
}

return 0;

}[/code]

I’ve tried multiple variations using lowercase bool and declaring isNotLegal as BOOL before assigning the conditional expression, but I don’t know why the compiler is complaining that BOOL is undeclared.

By the way I’m using Xcode 4.2 if that matters.

Hope someone can help :wink:


#2

Ah, I should change that. Sorry.

Here’s the thing: the BOOL type is defined in the Objective-C headers and is used by Objective-C programmers all the time. If you want to use BOOL in plain C code, you just have to include the correct header. Add a second #include:

#include <stdio.h>
#include <objc/objc.h>

I will change that to make it more clear. Sorry.