2

I am new to C++ (and quite new to programming overall) and I was reading my C++ college book ("Starting out with C++ Early Objects" 9th edition by Gaddis, Walters and Muganda) when I came across a note on the bool data type.

"NOTE: Notice that true and false do not have quotation marks around them. This is because they are variables, not strings."

Now, from what I've learned, variables can be changed. I understand that a variable of the bool data type would be a variable, but how come true and false are considered variables?

From my understanding, false is stored as an integer value 0 and true as an integer value 1. I tried assigning values x where x is 0<x<0 to a bool and they all output 1 which made me come to the conclusion that true is also everything other than 0 (in other words, true is the same as !false?).

So if this is true, how come 'false' is considered a variable and not a constant?

Henrik
  • 23
  • 3
  • 14
    Both `true` and `false` are keywords, and they are values. They are not variables. – Eljay Sep 29 '18 at 14:58
  • 7
    Apparently the authors were not careful in their word choice when they wanted to point out the difference between string literals and non-(string literals). Please consult https://stackoverflow.com/q/388242/11683 for better options. – GSerg Sep 29 '18 at 15:02
  • In their defence, they do also state at the start of that boolean variable section that _"`true` is a special integer variable whose value is 1"_ and same for `false` being 0, implying that they are constant. – Ian Sep 29 '18 at 15:06
  • Oh yeah, I saw that. They didn't bring up constants yet in this book so I guess that must be why. It confused me is all. Thank you for the helpful responses :) – Henrik Sep 29 '18 at 15:12
  • It's not what is usually called a constant either. It's a keyword denoting a literal. – Sebastian Redl Sep 29 '18 at 15:46
  • List of C++ keywords (including `true` and `false`): https://en.cppreference.com/w/cpp/keyword. `true` and `false` are Boolean literals, the same way `42` is an integer literal. – Richard Critten Sep 29 '18 at 16:47

1 Answers1

7

You’re using a book that shows clear lack of understanding of the subject matter by the author. That book is lying to you. Throw it in the trash.

true and false are Boolean literals: they are a straightforward way of writing down a value of type bool. "true" and "false" are string literals – and, unfortunately, C++ can help you shoot yourself in the foot by converting them to their address, and then to a Boolean. So you get this wonderful nugget:

bool b1 = "false"; // string contents don’t matter
assert(b1 == true);
using book = bool;
book b2 = false;
assert(b2 == false);

The asserts are a way of writing true statements in the code: they mean that, at the point they appear, the condition in the parentheses must be true.

true and false are stored in whatever way the compiler desires – this is an implementation detail and the standard places no requirements here, other than true having to convert to 1 in numerical context, and false having to convert to 0 there. Usually they are not stored as integers, but bytes (char), i.e.

assert(sizeof(int) > sizeof(bool));
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • Did you intend the pun with `book b2`? – GSerg Sep 29 '18 at 18:49
  • I don't manage to compile that code with a bool being defined as a string, I think you misinterpreted my question. I set the bools to integer values, not strings. Nevertheless, your answer was quite helpful, thank you :) – Henrik Sep 29 '18 at 20:37
  • 3
    You mean that the strings will convert to their adress in RAM, right? And then be converted to booleans. That means they will have a value that is not zero, and will therefore be true. That is what you are trying to say? – Henrik Sep 29 '18 at 20:42
  • 2
    @Henrik That’s correct. C and C++ have this nasty problem of automatically converting an array to the address of its first element. It seemed a good idea at the time… – Kuba hasn't forgotten Monica Sep 30 '18 at 12:22
  • @GSerg It was a very solid Freudian slip. Duly noted. – Kuba hasn't forgotten Monica Sep 30 '18 at 12:26