Skip to main content
Arduino

Timeline for Why can't I print boolean variable values (0 and 1) through println function?

Current License: CC BY-SA 4.0

14 events
when toggle format what by license comment
Jan 3, 2019 at 8:27 vote accept MrDeepThought
Jan 1, 2019 at 21:48 comment added Nick Gammon Like if you give a INT type number like 123, is it segregating the number to individual digits like 1,2 and 3 and then converting it to binary data to be interpreted by the Arduino - this is nothing to do with the Arduino per se. This question is about how do variables get printed which is a whole different question.
Jan 1, 2019 at 21:47 comment added Nick Gammon @Majenko (and others) see amended answer about what boolean is a typedef for.
Jan 1, 2019 at 21:46 history edited Nick Gammon CC BY-SA 4.0
Added more explanations.
Jan 1, 2019 at 11:27 comment added Majenko It could be nice to have a bool type overloaded print function though - maybe one that prints Y and N, or T and F perhaps.
Jan 1, 2019 at 11:14 comment added Majenko Oh, and assigning the return of digitalRead to a bool is perfectly valid, since "The value zero (for integral, floating-point, and unscoped enumeration) and the null pointer and the null pointer-to-member values become false. All other values become true.". And since LOW just happens to be 0 and HIGH 1 they map nicely to false and true in a bool.
Jan 1, 2019 at 11:04 comment added Majenko boolean is a typedef for bool. bool gets promoted to int, not byte ("the type bool can be converted to int with the value false becoming ​0​ and true becoming 1."). In Gnu C bool is a macro giving the internal (GCC specific) _Bool type, not a byte. In Gnu C++ bool is a natively supported internal type.
Jan 1, 2019 at 7:06 comment added MrDeepThought And if I am wrong can you please educate me with your knowledge on print() function
Jan 1, 2019 at 7:03 comment added MrDeepThought and since boolean returns a BYTE data type(bit 0 and bit 1) as an argument to print() it converts it into ASCII characters which are non printable by Arduino's serial monitor. Am i right?
Jan 1, 2019 at 7:03 comment added MrDeepThought Like if you give a INT type number like 123, is it segregating the number to individual digits like 1,2 and 3 and then converting it to binary data to be interpreted by the Arduino and then converting these digits to ASCII charcter representing that same number and then prints it on screen! And since print() takes all data types(being an overloaded function),
Jan 1, 2019 at 7:03 comment added MrDeepThought I am unable to get a complete insight into the print() or println() function from the Arduino site.
Jan 1, 2019 at 6:48 comment added Nick Gammon Depends what you mean by "the same thing". Internally they are. However to the compiler, different types may mean that it calls a different function for printing, particularly when you use overloaded C++ functions, which println is an example of.
Jan 1, 2019 at 6:14 comment added MrDeepThought aren't HIGH/LOW, true/false or 0/1 the same thing for the micro controller?Like these values are interpreted by the micro controller as the same bit 0 and bit 1.
Jan 1, 2019 at 2:09 history answered Nick Gammon CC BY-SA 4.0

AltStyle によって変換されたページ (->オリジナル) /