34

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?

Bugster
  • 4,033

6 Answers6

83

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.

Dynamic
  • 5,786
53

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!

gnat
  • 20,543
  • 29
  • 115
  • 306
6

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
else
  return false; //value is 0 or smaller than 0, thus is false

or

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;
else
  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)

1

The purpose here is clarity of intent. The return type is part of a functions interface and a bool tells you more about what to expect from the function than an int does.

Even BOOL is more expressive than int, even though it's the same type it at least shows your intent.

However, none of them is what I would recommend:

enum class UiCmd {QUIT, START_GAME};
kamikaze
  • 1,080
  • 2
  • 7
  • 9
-1

In programming you want to represent something from real life in code. Despite an int and bool can do the same, the subyacent idea is completely different: when using a bool the answer might be yes or not; and that's all, that's the intent. With integers you might represent quantities without decimal point. And in that same spirit, why would you choose an integer when a double can do the same? If an integer makes more sense than a double when modeling the problem, then you might choose an int.

fjrg76
  • 1
-2

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.

Pieter B
  • 13,310