Take the 2-minute tour ×

We had an assignment for our class where we had to create a Tic-tac-toe game. People like to complicate themselves, so they wrote complex games which included menus. At the end of the game, you had to have the option to play again or quit the program. I used an int variable for that, but I noticed some classmates using BOOLs.

Is it more efficient? What's the difference, between storing an answer that should only store two values in an int rather than storing it in a bool? What is the exact purpose of these variables?

share|improve this question
Not sure about efficiencies, but the purpose of an int is to store an integer and the purpose of a bool is to store a boolean value (true or false). Using a bool IMO reflects its use much better than using an int. –  George Duckett Apr 20 '12 at 10:36
In fact, before C++ and C99, C89 didn't have a Boolean type. Programmers would often typedef int Bool in order to make it clear that they were using a boolean. C++ integrated support for bool into the language, as did C99 with the (rather ugly) _Bool keyword. –  Charles Salvia Apr 20 '12 at 11:16
It's not about "know when to use bool", it's about why do we have different names for similar types (like length_t) and why it's important that compiler checks types. –  Abyx Apr 20 '12 at 15:45
Sometimes the answer is just 'taste'. I bet if you rewrite your same assignment right now this moment there will be several things different such as function parameter order and their names. Why didn't you write it the same parameter order or same name? Its because you just didnt or that was not very important and you just wrote whatever –  acidzombie24 Apr 21 '12 at 4:16
add comment

4 Answers

up vote 70 down vote accepted

When choosing variable types and variable names you want your intent to be as clear as possible. If you choose a bool (boolean) type, it is clear there are only two acceptable values: true or false. If you use an int (integer) type, it is no longer clear that the intent of that variable can only be 1 or 0 or whatever values you chose to mean true and false. Plus sizeof(int) will typically return as being 4 bytes, while sizeof(bool) will return 1.

share|improve this answer
Agreed. I think design intentions are more important. Only occasionally will you need to override them. –  ChrisF Apr 20 '12 at 11:02
To restate @AndrewFinnel's point: Bool is more self documenting. A variable you set to 0 or 1 could be a counter; a variable you set to true or false is clearly a flag. –  Scott Wilson Apr 20 '12 at 11:06
Bools prevent abuse of a variable for other uses. An integer can be set to values other than 0 or 1 to create additional states your code may not be aware of. –  Michael Shopsin Apr 20 '12 at 15:42
+1. It makes the intent/options clear. You could use any method you like to store the value including a string with the value "yes" or "no", but you should choose the one that makes MOST sense. In this case, that is a boolean. –  Craige Apr 20 '12 at 16:23
I thought booleans in C++ were the same size as ints, 4 bytes. –  DogDog Apr 20 '12 at 19:21
show 2 more comments

Because in the end, you're going to convert your integer to a boolean anyhow: "if (i=1) then play another game". In this situation (i=1) is converted to true or false: a boolean.

share|improve this answer
depends very much on which machine you are running on and the compilers involved. You may be surprised to learn than on IBM mainframes a single character flag with "Y" or "N" is the most efficient way to implement boolean logic. –  James Anderson Apr 24 '12 at 2:34
Of note, if (i = 1) is probably the very wrong thing to have in one's code. –  MichaelT Mar 2 at 3:06
add comment

Bool types are smaller than Int types, thus use less room in memory. Depending on the system you're compiling on/for, an Int can be 4 - 8 bytes, whereas a Bool is 1 byte (as can be seen in this MSDN article)

Couple this with some of the aspects of KISS and good program design, and it becomes obvious why it's better to use a bool to store a variable that will only ever have 2 values.

Why over complicate things with an object that can store a wide range of values, when you are sure that you only ever need to store 1 of 2 different values?

What happens in the system that uses an int, if you store 75 in there? If you've added extra conditionals

if (value >= 0 )
  return true;  //value is greater than 0, thus is true
  return false; //value is 0 or smaller than 0, thus is false


if (value == 0)
  return false;  //value is greater than 0, thus is true
else if (value == 1)
  return true; //value is 0 or smaller than 0, thus is false

then you're covered for this situation. But if you haven't, then you're not.

You could also have a case (depending on how you're changing the value of the int) where you have a buffer overrun, and the value "resets" back to 0 or the lower bound of your int (which could be somewhere in the region of -127 to −9,223,372,036,854,775,808, depending on your target architecture) what happens in your code then?

However, if you used a bool you could use something like this:

if(continueBool == true)
  return true;
  return false;

Or even:

return (continueBool== true) ? true : false;

or even:

return continueBool;

Depending on your compiler, there might be optimizations that it can perform on code that uses Bools to store mapped true/false values. Whereas, there might not be optimizations it can perform for Ints used to store mapped true/false values.

We've also got to remember that C++ (along with C, Assembly and FORTRAN) is used to write highly efficient, small and fast code. So, it would be better to use a Bool in this instance - especially if you are being marked on your use of variables, memory, cache or processor time.

A similar question would be: why would I store an integer (value) in a float? Answer: You shouldn't, because there's no point.

Long story short: As your teacher/tutor/lecturer/professor to go over the sizes of different value types with you (in case you missed it), and why they're important in Software Development.

I hope that helps as a starting point (I also hope that it doesn't come across as pedantic)

share|improve this answer
Thanks for the detailed answer, it's great to see some well-placed examples. –  Bugster Apr 20 '12 at 11:24
Unnecessary use of if() award. Just write return value >= 0; for the first example. –  zvrba Apr 20 '12 at 13:14
I wasn't sure on the level of OP's understanding of syntax. Sometimes it's rewarding to be a little more verbose than normal - especially since OP mentioned that it was an assignment –  Jamie Taylor Apr 20 '12 at 13:21
Note that continue is a reserved keyword in both C and C++. –  user29079 Apr 20 '12 at 13:34
Not to disagree -- but just to point out that saving three bytes by choosing a bool over an int is not going to make any noticeable difference to most programs. Don't worry about efficiency until you really have a performance problem! –  James Anderson Apr 24 '12 at 2:31
show 2 more comments

It seems in all the (till now) collected answers no-one caught the fact that the OP spoke about BOOL not bool.

Since the question is tagged C++, it must be noted that:

  • int is an integer that ranges from INT_MIN to INT_MAX — macros defined in <climits> whose values depend on the architecture of the hosting machine. In C++ these values are also accessible as std::numeric_limits<int>::min() and ...:max() respectively). The behavior of boolean operators applied to int treat 0 as false and everything else as true.
  • BOOL is just an hint suggesting a boolean behavior for a int. It is defined in <cstddef> as

    #define BOOL int
    #define TRUE 1
    #define FALSE 0
  • BOOL is so nothing more than syntactic sugar, for what -by the compiler- it is nothing more than an int. It is something C programmer use, but C++ programmers should avoid, since C++ has bool.

  • bool is a language integral type whose supported values are just true and false. When converted to int true becomes 1 and false becomes 0.

The important aspect is that it is more safe against programming mistakes:

BOOL a = FALSE;  // in fact int a = 0;
a = 5; //now a == 5 -- what does it mean?;

is impossible to code with proper bool type:

bool a = false;
a = 5; // error: no bool(const int&) available.

Using BOOL instead of bool is just a bad habit inherited from a glorious past no-ones is actually still able to forget, thus creating old problem for a less glorious tomorrow.

Language teachers should seriously think about it!

share|improve this answer
BOOL isn't part of the C++ nor the C language. BOOL with upper-case letters is the most common way booleans were implemented in C, back in the old days when C had no boolean type. For example, the Windows API will define BOOL. Further, there is no telling how BOOL is defined, some applications might define it as a one bit long bit-field. You can't assume that it is always equal to int just because some specific library defines it that way. –  user29079 Apr 20 '12 at 13:30
+1. Perhaps he actually did mean BOOL and not bool. Perhaps BOOL can be implemented in potentially different ways, though Codereview likely doesn't know that if he's asking this sort of question. He sees that it is defined as an int, so he naturally asks why can't he simply use int. –  Neil Apr 20 '12 at 13:37
@Lundin: in general sense you're correct, but consider that this is an answer that leave inside the question's scope, where the OP spoke about BOOL and int equivalence. –  Emilio Garavaglia Apr 20 '12 at 13:51
Even so, the idea of using BOOL or bool to signify intent still applies. –  Andrew Finnell Apr 20 '12 at 19:36
@zvrba: true, but this is due to the way MS decided to implement bool in its own compilers. It is valid only for MS compilers working for Intel processors. Note that, for Intel platform, every integral type shorter than 32 bits requires a masking on input or output. But char[] are still used and not necessarily always replaced by int[] –  Emilio Garavaglia Apr 30 '12 at 15:44
show 7 more comments

Your Answer


