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?
6 Answers 6
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.
-
7Agreed. I think design intentions are more important. Only occasionally will you need to override them.ChrisF– ChrisF2012年04月20日 11:02:23 +00:00Commented Apr 20, 2012 at 11:02
-
10To 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 C Wilson– Scott C Wilson2012年04月20日 11:06:16 +00:00Commented Apr 20, 2012 at 11:06
-
2Bools 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– Michael Shopsin2012年04月20日 15:42:08 +00:00Commented Apr 20, 2012 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– Craige2012年04月20日 16:23:55 +00:00Commented Apr 20, 2012 at 16:23
-
I thought booleans in C++ were the same size as ints, 4 bytes.DogDog– DogDog2012年04月20日 19:21:29 +00:00Commented Apr 20, 2012 at 19:21
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 fromINT_MIN
toINT_MAX
— macros defined in<climits>
whose values depend on the architecture of the hosting machine. In C++ these values are also accessible asstd::numeric_limits<int>::min()
and...:max()
respectively). The behavior of boolean operators applied toint
treat0
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++ hasbool
.bool
is a language integral type whose supported values are justtrue
andfalse
. When converted toint
true
becomes 1 andfalse
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!
-
9BOOL 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– user290792012年04月20日 13:30:40 +00:00Commented Apr 20, 2012 at 13:30
-
1+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– Neil2012年04月20日 13:37:54 +00:00Commented Apr 20, 2012 at 13:37
-
1@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– Emilio Garavaglia2012年04月20日 13:51:50 +00:00Commented Apr 20, 2012 at 13:51
-
Even so, the idea of using BOOL or bool to signify intent still applies.Andrew T Finnell– Andrew T Finnell2012年04月20日 19:36:32 +00:00Commented Apr 20, 2012 at 19:36
-
1@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– Emilio Garavaglia2012年04月30日 15:44:17 +00:00Commented Apr 30, 2012 at 15:44
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)
-
4Unnecessary use of if() award. Just write
return value >= 0;
for the first example.zvrba– zvrba2012年04月20日 13:14:41 +00:00Commented Apr 20, 2012 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 assignmentJamie Taylor– Jamie Taylor2012年04月20日 13:21:12 +00:00Commented Apr 20, 2012 at 13:21
-
2Not 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– James Anderson2012年04月24日 02:31:22 +00:00Commented Apr 24, 2012 at 2:31
-
@James: also in many cases you won't save three bytes because the next variable, unless it's another bool or a char, will probably get aligned to a four byte boundary.JeremyP– JeremyP2012年04月24日 10:36:41 +00:00Commented Apr 24, 2012 at 10:36
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};
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.
-
1this doesn't seem to offer anything substantial over points made and explained in top answer here that was posted about 6 years agognat– gnat2018年03月28日 05:57:57 +00:00Commented Mar 28, 2018 at 5:57
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.
-
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– James Anderson2012年04月24日 02:34:03 +00:00Commented Apr 24, 2012 at 2:34
-
4Of note,
if (i = 1)
is probably the very wrong thing to have in one's code.user40980– user409802014年03月02日 03:06:35 +00:00Commented Mar 2, 2014 at 3:06
int
is to store an integer and the purpose of abool
is to store a boolean value (true
orfalse
). Using abool
IMO reflects its use much better than using anint
.typedef int Bool
in order to make it clear that they were using a boolean. C++ integrated support forbool
into the language, as did C99 with the (rather ugly)_Bool
keyword.bool
", it's about why do we have different names for similar types (likelength_t
) and why it's important that compiler checks types.