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?
Best Answer
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
orfalse
. If you use anint
(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 meantrue
andfalse
. Plussizeof(int)
will typically return as being 4 bytes, whilesizeof(bool)
will return 1.