Go Down

Topic: Convert int to boolean (Read 2052 times) previous topic - next topic

BobW

Why does the code below print 5 instead of 2 ?
If I store 2 or 3 to a BOOLEAN variable, wouldn't it make the variable TRUE (1) ?

If I "bang" (negate) x and y TWICE before printing (!!x + !!y), I get 2 as I would expect.

Does this mean that boolean variables are really stored as whole bytes?

Code: [Select]
boolean x = 2;
boolean y = 3;
int z = 0;
 Serial.begin(9600);
 z = x + y;
 Serial.println(z, DEC);

PaulS

On the Arduino, boolean is a typedef, not a native type (like int). It's a uint8_t in disguise.

Quote
Does this mean that boolean variables are really stored as whole bytes?


Yes, it does.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy