0

Relizing there's no such thing as a BOOL datatype, take the following:

std::cout << (1>2); //<<-- prints 0

Assuming this false comparison is a 0, what datatype deos the result of a comparison reduce to? Doing a quick google search doesn't yield any results. My best guess it that it's an unsigned char because it's the smallest most basic datatype where 0 truly represented as 0x00. I don't want to assume anything because I'm not sure what voodoo std::cout does to the value to make it a printable character.

Chad Harrison
  • 2,836
  • 4
  • 33
  • 50

4 Answers4

10

The type of the result of all relational operators (<, >, <=, >=) is bool:

The operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) all yield false or true. The type of the result is bool.

An object of type bool has the values true or false.Under integral promotion, a bool can be converted to an int where false becomes 0 and true becomes 1:

A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true becoming one.

bool is an integral type, which the standard says are represented by use of a "pure binary numeration system". The footnote that describes this representation is fairly unclear as to how it maps to the values true and false, but you could assume that they are implying that the value representation for 0 would be all 0 bits:

A positional representation for integers that uses the binary digits 0 and 1, in which the values represented by successive bits are additive, begin with 1, and are multiplied by successive integral power of 2, except perhaps for the bit with the highest position. (Adapted from the American National Dictionary for Information Processing Systems.)

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • I thought `bool` was a typedef? – Chad Harrison Jan 17 '13 at 21:59
  • 2
    @hydroparadise, Maybe in C with ``. – chris Jan 17 '13 at 22:00
  • 5
    No, it's a real type. `BOOL` is just a crappy WinAPI typedef that has nothing to do with anything. – Puppy Jan 17 '13 at 22:00
  • so it's truly `0x0` as in it only takes up 1 bit of space or is it still a byte? its been a looooong time since ive messed w c++. – Chad Harrison Jan 17 '13 at 22:03
  • @hydroparadise, standard C (C99 or newer) has `bool`, too, but it's a typedef to `_Bool` for safely compiling older code. It's still a real boolean type. – Carl Norum Jan 17 '13 at 22:03
  • @hydroparadise: Note that sftrabbit's new text about what a `bool` can convert to is _not_ the same as what space the `bool` takes up in memory and what physical values it holds. – Lightness Races in Orbit Jan 17 '13 at 23:59
  • @hydroparadise: in memory a `bool` is at least a byte, and is permitted to be more. – Steve Jessop Jan 18 '13 at 01:39
  • 1
    @sftrabbit: `bool` is an integer type, so it's required to use a "pure binary representation". But the C++ standard is vague in its definition of that (compared with the C standard, anyway). It's not clear to me that the authors really thought about how the definition in the footnote, "begin with 1" is applied to `bool`. You'd think integer types would be those with integer values, but no. Should we assume that because `false` converts to 0, it "is" 0 and therefore is required to be represented by a clear bit somewhere in the object representation? Maybe, but it doesn't actually say that. – Steve Jessop Jan 18 '13 at 01:43
  • @SteveJessop Good find! Added it to my answer. – Joseph Mansfield Jan 18 '13 at 12:11
6

There's no standard BOOL type, but bool is a standard fundamental type:

[C++11: 3.9.1/6]: Values of type bool are either true or false. [..]

As for the result of your relational comparison:

[C++11: 5.9/1]: The relational operators group left-to-right. [..] The operands shall have arithmetic, enumeration, or pointer type, or type std::nullptr_t. The operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) all yield false or true. The type of the result is bool.

Note that this is not the same in C, in which there is no built-in type bool and the result of relational comparisons is of type int:

[C99: 6.5/8]: Each of the operators < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to) shall yield 1 if the specified relation is true and 0 if it is false. The result has type int.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
2

The C++ standard, section 5.9 Relational operators, paragraph 1 says:

The type of the result is bool.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
0

This is not a direct answer to your question:

as said here it is bool in c++ and int in c but you i think the part which you would think of is how much memory it takes to save a comparison result ?

  • as you know a data type defines how much memory to be allocated.

  • Note: sometimes it(data type to memory size definition) is different from compiler/processor architecture to another for example in embedded systems environment people used to talk about and define data types in projects using number of bits e.g typedef unsigned char uint8; instead of using standard data types directly so it would be easy to port to another compiler/target processor

  • you should look at this: Why is a char and a bool the same size in c++?

  • you should look at this also http://www.cplusplus.com/doc/tutorial/variables under "Fundamental data types" section a table of each data type and its size and range but he noted :

The values of the columns Size and Range depend on the system the program is compiled for. The values shown above are those found on most 32-bit systems. But for other systems, the general specification is that int has the natural size suggested by the system architecture (one "word") and the four integer types char, short, int and long must each one be at least as large as the one preceding it, with char being always one byte in size. The same applies to the floating point types float, double and long double, where each one must provide at least as much precision as the preceding one.

  • as noted by sftrabbit about the standard in his answer that the standard is abstract enough and not detailed thus i think that the size of bool in memory is an implementation specific which may be different from c++ compiler to another , check Nawazs' answer here: How a bool type variable is stored in memory? (C++)
Community
  • 1
  • 1
Abdurahman
  • 628
  • 8
  • 16