Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cybrscot

macrumors 6502
Original poster
Dec 7, 2010
282
0
Somewhere in Southeast Asia
Okay, in chap 7.6 "type definitions" it says "there's a better way to set up a Boolean type, though, using a feature known as a type definition.
typedef int Bool;

So, I didn't exactly remember what a Boolean was all about so I went back to chapter 5.2 to look again. (There is only 1 page about Boolean) I've not had to use it in any of the exercises thus far. (I've done them all up to now)

This is all it says about Boolean Values:
Book: "C's lack of a proper Boolean type can be annoying, since many programmers need variables that can store either false or true. (Recognizing this problem, newer versions of C++ provide a built in Boolean type) We can always simulate a Boolean variable by declaring an int variable then assigning it 0 or 1."

That's the entire explanation of Boolean values. Of course, chapter 7 says, "there's a better way to set up a Boolean type" which discredits what it already taught me in chapter 5.2, if there's a better way why did they teach me the worse way first?? ARGH!:rolleyes:

Based on the above, I guess a Boolean value is just a variable that can store either 1 or 0, (true or false). But I don't understand why I'd need to do that? Or when?

So what is the point of a Boolean value? How and when and why is it used? Unless I'm missing something, I don't really think that paragraph taught me about Boolean values in enough detail.

So now in Ch 7, I'm learning a better way to set up a Boolean Value, which is something that they really haven't taught me to use in the first place. Am I missing something here?
 
Boolean just means "true or false". You implicitly use boolean types every time you write an if statement, a for loop, etc...

Both of the sections you mentioned are equivalent, one just provides a tip on providing a convenient name for using an int as a boolean.
 
Boolean values are very natural. For example, in C, most functions describe their return as something like this: 0 on success, non-zero on failure. This condition (that is {success, failure}) is represented as a boolean variable. Any truth statement is represented by a boolean.

By the way: most C (and Unix) functions in fact return a range of values; since they have an int of space, they use the other values for different return states. It's usually the case that success has one value though--the others are failures.

Their explanation is complete--you can construct a boolean value by just declaring an int and using 0 for true and 1 for false (or vice-versa).

Of note: many things you see in code actually represent some sort of "boolean" state: that is, true or false. For example, if a loop condition is (i < 5), then each time this is evaluated, its result is in {true, false}, making the comparison a boolean-resulting operation. Similarly for (ptr == NULL), etc.
 
You'd normally use something like this (in pure C, modern C++ and Objective-C already have booleans)

Code:
typedef enum {FALSE=0, TRUE} Boolean;
 
cybrscot, please try to see if you can use Amazon Look Inside to read the description of Boolean in "Learn C on the Mac" or Google Books it starts around page 90 and goes on for a few pages use the search function and look for "Boolean".

Just a test to see if you understand it better when explained differently.

B
 
As an Amazon Associate, MacRumors earns a commission from qualifying purchases made through links in this post.
Of course, chapter 7 says, "there's a better way to set up a Boolean type" which discredits what it already taught me in chapter 5.2, if there's a better way why did they teach me the worse way first?? ARGH!:rolleyes:

Because you wouldn't have understood typedef's without an explanation, and the author didn't want to explain it all at that point. You should expect that some things will be revisited with new information as your foundation of knowledge grows.

If you had to explain where babies come from to a 5-year old, do you think they'd understand it? If they understood the basic mechanics, do you think they'd understand all the social aspects (paternity, teen pregnancy, single mothers, marriage, divorce, etc.)?
 
Based on the above, I guess a Boolean value is just a variable that can store either 1 or 0, (true or false). But I don't understand why I'd need to do that? Or when?

So what is the point of a Boolean value? How and when and why is it used?

At their heart, computers are just piles of electronics that handle the presence or lack of a voltage. On or off. 1 or 0. That's where binary code comes from. That's why ancient computers you see in museums had lots of toggle switches (on or off) and blinking lights (on or off).

Virtually everything always breaks down, eventually, to an on-or-off, do-or-don't, true-or-false, yes-or-no situation, in order for the processor electronics to handle it. Ultimately it all comes down to boolean, binary, bits: 1 or 0.

There's even an entire set of Boolean math that defines operations that you can do with bits. Like AND or OR or NOT operations.

Any kind of decision making that your program makes, has to eventually be evaluated in Boolean terms. "if X is Y then do Z", for example, has exactly two possibilities: X is Y, or X is not Y. There's your yes/no, on/off, true/false. Boolean.

When your program asks the user "Do you wish to try again?" and the user can enter Yes or No, you'll use Boolean logic to determine what to do. "If the user said yes..." True or false? That's Boolean.

When your program asks the user "There are 3 options: do you want A, B, or C?" You will still use Boolean logic in your code: If the user said A.... (did he or didn't he? True or false? Boolean.) else if the user said B... else if the user said C...

This can take the form of an if statement (if X then Y) or a switch/case statement (which, ultimately, works out to the same thing.)

When you ask your code to loop around to do something 5 times, a comparison occurs (has it been 5 times yet? Yes or no? True or false? Boolean.)

Even adding two numbers together, when the computer breaks it down, becomes a bunch of boolean operations using binary.
 
Last edited:
Their explanation is complete--you can construct a boolean value by just declaring an int and using 0 for true and 1 for false (or vice-versa).
I wouldn't use 0 for true, since statements like
Code:
while (true) {
    /* Do something cool */
}
wouldn't work as expected when reading it. Also, when moving to C99 (which defines a boolean type), it might break some functionality or introduce bugs since it defines true as 1 and false as 0.
 
Okay, in chap 7.6 "type definitions" it says "there's a better way to set up a Boolean type, though, using a feature known as a type definition.
typedef int Bool;

That's the entire explanation of Boolean values. Of course, chapter 7 says, "there's a better way to set up a Boolean type" which discredits what it already taught me in chapter 5.2, if there's a better way why did they teach me the worse way first?? ARGH!:rolleyes:

Worse way ? You're not understanding what typedef is. Re-read your chapter 7 again until you do.

Both ways produce the same exact result. Think of it this way.

Chapter 5.2 :

Code:
#include <stdlib.h>

int main(int argc, char ** argv)
{
    int istrue = 1;
    if(istrue)
        printf("It's true!\n");

    return EXIT_SUCCESS;
}

Chapter 7 :

Code:
#include <stdlib.h>

typedef int bool;

int main(int argc, char ** argv)
{
    bool istrue = 1;
    if(istrue)
        printf("It's true!\n");

    return EXIT_SUCCESS;
}

Both are exactly the same code. There is no better or worse way here, just a more readable way in that the Chapter 7 method tells you istrue is supposed to be a boolean, but in both cases, it's just an int variable.

Seriously, change your book. At this point, I don't even think you've used it since you've asked so many questions here, you probably learned more about C from us than you did from that book.
 
Last edited:
Just include stdbool.h which includes true, false definitions, this header was introduced with C99.
 
cybrscot, please try to see if you can use Amazon Look Inside to read the description of Boolean in "Learn C on the Mac" or Google Books it starts around page 90 and goes on for a few pages use the search function and look for "Boolean".

Just a test to see if you understand it better when explained differently.

B

Yeah, I just downloaded the ebook. It has half the chapters of my current book, looks easier, but also less detail oriented from what I can tell. I'll give it a whirl, maybe it will be easier to pick up on some things. It also requires that I use Xcode. I'm excited about that as I feel ready to leave Text Wrangler and Terminal and get familiar with Xcode, as I will eventually be operating in that environment solely. (I hope)

P.S. Does Tourettes Syndrome apply to written language as well as spoken language? It seems that Mr. WRX is checking in on my threads again. Unable to control what he writes/says as he accused me of not using my book! First it was accusations that I was trying to get everybody here to do my "homework" for me and that I would "disappear" at the end of some mythical "semester". Now this?? However it may very well be true that much of the credit for what I've learned goes to the people on this forum that have helped me, no doubt about that.
 
As an Amazon Associate, MacRumors earns a commission from qualifying purchases made through links in this post.
Seriously, change your book. At this point, I don't even think you've used it since you've asked so many questions here, you probably learned more about C from us than you did from that book.

Haha, that's probably true :p

OK, so think of a typedef (a "type definition") as a way to create a new variable type, to help you document what's really going on in your code. Most code works with values of different kinds, but those different kinds of data tend to boil down to the same numeric types. For example, you might have a program that keeps track of, I dunno, the number of books you own, the number of coffees you drink each day, and your best video game scores. All of those can be represented as integers. As far as the compiler is concerned, any integer value can be freely added, multiplied, etc. with any other integer value. But does it really make sense to multiply the number of coffees by your high score? Probably not. Even though they're both integers, your program probably does not plan for them to interact.

So you might make your code a little bit easier to read by using typedefs:

typedef int highScoreValue;
typedef int numberOfCoffees;

Now you've "invented" a new data type (but sssssh, it's really just an integer) that you can use to create new variables:

int x = 5;
becomes
numberOfCoffees x = 5;

It just makes your code easier to read and helps YOU, the programmer, understand that one is not the same as the other. Otherwise, in a large program with ints, ints, ints, everywhere, you might get confused.

Note that C will still treat it exactly like an int and if you really want to you can still divide the coffees by the high scores. There are some programming languages that are more "strongly typed" (Ada is one example) that WILL NOT let you compare coffees to high scores once you define them as two separate, different types that shouldn't play together.

There are other uses for typedefs which you will get into once you learn more about structures and enumerations and stuff, but that's the basic idea.
 
I'm excited about that as I feel ready to leave Text Wrangler and Terminal and get familiar with Xcode, as I will eventually be operating in that environment solely. (I hope)

=/ Hm. That part doesn't excite me. What you're working on now is pretty short programs with a small number of files (likely 1-2). You're not using a ton of third-party libraries, you're not having complex build situations, etc. An IDE is way overkill at this point. I would encourage you to stick to the terminal until you're spending a significant time setting up builds vs. actually writing code. XCode only runs on the Mac, has limited language support (though people do add support for others with plugins), and really obscures the complexity of what's going on when it's compiling, linking, debugging, etc. If you're comfortable with the commandline tools and performing those actions yourself it will make you much more versatile, and you'll be able to say that you're a programmer, not only a mac programmer.

-Lee

P.S. There is a "user lists->add to ignore list" option on everyone's profile page, if someone is rubbing you the wrong way. I won't comment beyond that, but probably better to just ignore something than having a flamewar ignite.
 
It has half the chapters of my current book, looks easier, but also less detail oriented from what I can tell.

The details are usually what is hanging you up, so you can leave them for later. Get the big picture, assemble your toolkit, you can sharpen your tools later.

It also requires that I use Xcode. I'm excited about that as I feel ready to leave Text Wrangler and Terminal and get familiar with Xcode, as I will eventually be operating in that environment solely. (I hope)
I'll echo what lee1210 says. An IDE is overkill at this point. The book may suggest that you use Xcode, and show you how to do things with it, but you should try it both ways. There is nothing in what I've seen in the book that requires Xcode. Most of the code samples are plain old regular C.

It just makes your code easier to read and helps YOU, the programmer, understand that one is not the same as the other. Otherwise, in a large program with ints, ints, ints, everywhere, you might get confused.

Another key reason for it is abstraction.

What if initially you thought it would be appropriate for highScoreValue to be an int, but as time progresses your players get so good at the game that you are about to overflow int and want to switch it to unsigned long int. If you used typedef, all variables of type highScoreValue can be changed to the new type with one edit. Otherwise, you'd have to make the change piecemeal.

B
 
Last edited:
=/ Hm. That part doesn't excite me. What you're working on now is pretty short programs with a small number of files (likely 1-2). You're not using a ton of third-party libraries, you're not having complex build situations, etc. An IDE is way overkill at this point. I would encourage you to stick to the terminal until you're spending a significant time setting up builds vs. actually writing code. XCode only runs on the Mac, has limited language support (though people do add support for others with plugins), and really obscures the complexity of what's going on when it's compiling, linking, debugging, etc. If you're comfortable with the commandline tools and performing those actions yourself it will make you much more versatile, and you'll be able to say that you're a programmer, not only a mac programmer.

-Lee

P.S. There is a "user lists->add to ignore list" option on everyone's profile page, if someone is rubbing you the wrong way. I won't comment beyond that, but probably better to just ignore something than having a flamewar ignite.

Sounds good, I'll try to go back and forth between both and see if that broadens my skill set some. Thanks for the "tip". It's exactly what I need.
 
P.S. Does Tourettes Syndrome apply to written language as well as spoken language? It seems that Mr. WRX is checking in on my threads again. Unable to control what he writes/says as he accused me of not using my book! First it was accusations that I was trying to get everybody here to do my "homework" for me and that I would "disappear" at the end of some mythical "semester". Now this?? However it may very well be true that much of the credit for what I've learned goes to the people on this forum that have helped me, no doubt about that.

If you can get past the tone in some of his paragraphs, he actually makes some good programming points.

Regardless, keep plugging away! When I had to learn C++ (coming from C), it took me a good 6 months before I had some clue to what I was doing. And I am a software developer by trade.;)
 
P.S. Does Tourettes Syndrome apply to written language as well as spoken language? It seems that Mr. WRX is checking in on my threads again. Unable to control what he writes/says as he accused me of not using my book!

Look, I was being helpful in both your threads. I didn't accuse you of anything, I'm simply pointing out that you and that book aren't jiving. I meant you "didn't use it" not as to mean you're not reading it, I meant it to as to say you just aren't understanding the concepts the way they are explained in it.

That book simply isn't good for you.
 
Look, I was being helpful in both your threads. I didn't accuse you of anything, I'm simply pointing out that you and that book aren't jiving. I meant you "didn't use it" not as to mean you're not reading it, I meant it to as to say you just aren't understanding the concepts the way they are explained in it.

That book simply isn't good for you.

We aren't used to using the written word enough to always express tone clearly. Especially online when we're often quickly jotting a note to a stranger. Intention and subtext are generally lost, and on the other side we're used to getting the worst from others online. If these were face to face conversations I expect we'd have far fewer misunderstandings and much greater civility. There are some users that are very helpful and mean no harm, but simply have a tone that is easily misinterpreted.

It's probably best for us to remember that if someone responds at all they likely mean well, and that when we post others may assume the worst due to conditioning.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.