1

In C++, is there any difference between assigning a bool to true and assigning it to 1(or any non-zero integer)?

I have seen many competitive programmers use 1 instead of true even when the code is written in advance, so I want to know if there is any benefit besides simply being faster to type.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 19
    *I have seen many competitive programmers use 1 instead of true* -- The last thing you want to use as a guide in writing programs properly is to look at competitive programming examples. – PaulMcKenzie Jun 27 '23 at 16:38
  • 1
    @PaulMcKenzie I know I shouldn’t follow any competitive programming practices when writing normal code; I just want to know if there’s any reason for this specific thing. – Youssef Gamil Jun 27 '23 at 16:40
  • The other advantages include that it makes you look cool. there is no semantic difference between using 1 and true. Non-zero values are implicitly converted to true: https://en.cppreference.com/w/cpp/language/implicit_conversion – Andy Turner Jun 27 '23 at 16:42
  • You will only notice a difference if other people look at your code. They may describe the code as "obfuscated" or "hostile". – Drew Dormann Jun 27 '23 at 16:43
  • @DrewDormann That’s actually exactly why I asked this question. I kept seeing it and wondered if there was any real reason to write it when “true” is much clearer. – Youssef Gamil Jun 27 '23 at 16:45
  • 2
    The two possible reasons I can see for using `0` or `1` instead of `false` and `true`: The first is thinking that it's somehow "faster", which it really isn't (not to build and not to run); The second is pure laziness. Both of these reasons are bad. – Some programmer dude Jun 27 '23 at 16:46
  • *so I want to know if there is any benefit besides simply being faster to type* -- Then the coder might as well used `int` -- that is one less letter to type than `bool`, and the code becomes "clearer" in one sense, and that it makes sense to assign 0 or 1 to an `int`. – PaulMcKenzie Jun 27 '23 at 16:46
  • 3
    Competitive coding is not to be taken as a source of learning how C++ works. I can get really frustrated by all the junk shown on those sites. Writing fast code comes after you learned how to write correct code (that is readable/maintainable and testable). And then in practice the speed is found in algorithms. (which you then can fine tune using profiling) – Pepijn Kramer Jun 27 '23 at 16:46
  • 1
    C++ should be learned from good sources, like : A [recent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or have a go at https://www.learncpp.com/ (that's pretty decent, and pretty up-to-date). For C++ reference material use : [cppreference](https://en.cppreference.com/w/). And after you learned the C++ basics from those sources, look at the [C++ coreguidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) regularely to keep up-to-date with the latest guidelines. – Pepijn Kramer Jun 27 '23 at 16:47
  • `bool` is in no way required to be compared to an exact `int` value coming out as `1`. Any `int x != 0` will do. – πάντα ῥεῖ Jun 27 '23 at 16:47
  • @πάνταῥεῖ In my question, I said “1(or any non-zero integer).” – Youssef Gamil Jun 27 '23 at 16:48
  • It's also possible that the developer trained with a language that doesn't have `true`, such as C89/90 – Drew Dormann Jun 27 '23 at 16:49
  • Here are a couple of reasons: 1) You are not allowed to use the `bool` type. 2) You want to show off your age (dating back to when `bool` wasn't a type). – Thomas Matthews Jun 27 '23 at 17:08

2 Answers2

5

... and assigning it to 1(or any non-zero integer)?

In your simple example, the effect will be the same.

// all true:
bool foo = true;
bool bar = 1;
bool baz = 123;

is there any difference?

The one I come to think about is readability/intent. Assigning 1 to a variable displays that the programmer intends this to be a variable carrying a numeric value. After a closer inspection of the code, I will notice that it's assigned to a bool (and I will promptly change it to true before continuing to read the code).

In the not so simple cases, you'll instead get a surprise:

#include <iostream>

void foo(bool) { std::cout << "bool\n"; }
void foo(int) { std::cout << "int\n"; }   // oups someone added an overload

int main() {
    foo(1);   // I use 1 instead of true because it's so much shorter
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
2

From the C++ 17 Standard (7.3.15 Boolean conversions)

1 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer-to-member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.

So there is no difference between the two initializations shown in the question.

Nevertheless for readability of the code it is much better to use boolean literal true instead of an arithmetic non-zero constant.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335