1

I know that for the Arduino (and C in general) that for comparison sake, "false" is 0, while any non-zero integer is "true". The question that I have is does the bool data type "return" a concrete integer for "true" or "false"?

When I tried Serial.print(true == 1), it printed 1 (which I assumed to be "true"), but when I tried Serial.print(true == 2), it printed 0 (which I assumed to be "false"). When I tried Serial.print(false == 1), it printed 0 and Serial.print(false == 0) printed a 1.

I didn't know if "true" always evaluated to 1 and "false" always evaluated to 0, or if this is not supposed to happen.

asked Apr 13, 2019 at 19:33
2
  • Never use if(flag==1), don't even use if(flag==true), but use if(flag), when the variable flag is a bool or boolean. The bool is part of the c++ language and the boolean is made up by arduino. The Serial.print function prints integers and floats, but sadly not "true" or "false" for a bool, so it uses the integer value and prints "1" or "0". Commented Apr 13, 2019 at 21:43
  • @Jot, I was mainly seeing if it would be the same because I have function foo(int tf) where tf is either a 1 or a 0, and merely wanted to see if tf == true would be the same as tf == 1. But if the printer is translating true to 1, then it sounds like what I'm proposing is a bad idea. Commented Apr 14, 2019 at 11:09

3 Answers 3

1

If you look at Arduino.h, you'll see true, false and bool defined as:

#define true 0x1
#define false 0x0
typedef uint8_t boolean;

Using a simple test sketch, it appears that comparing true or false to anything besides a 1 or 0 does not "work".

void setup(){
 Serial.begin(9600);
 if(false == 0){Serial.println("false=0");}
 if(false == 1){Serial.println("false=1");}
 if(false == 2){Serial.println("false=2");}
 if(false == -1){Serial.println("false=-1");}
 if(true == 0){Serial.println("true=0");}
 if(true == 1){Serial.println("true=1");}
 if(true == 2){Serial.println("true=2");}
 if(true == -1){Serial.println("true=-1");}
}
void loop(){}
answered Apr 13, 2019 at 20:13
0

true is indeed 1, and false is 0.

This follows your tests:

(true == 1) -> prints 1, because 1 == 1
(true == 2) -> prints 0, because 1 != 2
(false == 1) -> prints 0, because 0 != 1

In C true and false are defines and a bool/boolean is a typedef (mostly to an int, unsigned int or unsigned char), where normally false is defined to 0 and true to 1 OR 255. So you cannot rely on a specific value.

In C++ the values of true and false are resp. 1 and 0, when casted to an int.

answered Apr 13, 2019 at 20:11
0

A 'bool' is defined data type based on (and identical to) an int. It's reason for existence is documentary: it's name describes how you intend to use it. There is no other difference.

'true' and 'false' are the constants 1 and 0, respectively. If you assign 'true' to a bool or any other integer type, it will get the value '1'. If you make a comparison or any other logical test (one that evaluates to true or false), that test results in a numerical value, either 1 or 0.

However you can evaluate (i.e. test) any integral value logically (true or false): if it is equal to zero the result will be false (0); otherwise the result will be true (1) - no matter what non-zero value the expression itself has. That is because the logical operators, '==', '!=', '>', etc., are (similar to) functions which can only return one of two values 'true' and 'false', regardless of the values of their inputs. That is another meaning of the bool datatype: when it is applied to a function, it implies to the compiler and to the reader that this function returns either true or false.

answered Apr 14, 2019 at 19:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.