2

I sometimes see this in a C program (I'm using the C18 compiler):

unsigned char someValue = getSomeDataFromSomewhere();
if (someValue) {
    doStuff();
} else {
    doOtherStuff();
}

I know what happens when you give an if loop a boolean (unsigned in the C18 compiler), but what happens when you put a non-boolean in?

My guess: it does doStuff() when the value isn't zero, and doOtherStuff() when the value is zero. But I don't know this, so I'd like to get some reference.

2 Answers2

7

Simply put -- if it is non-zero, it is true. If it is zero, it is false.

K Scott Piel
  • 4,320
  • 14
  • 19
5

your guess is right:

from ยง6.8.4.1 of WG14/N1256

the first substatement is executed if the expression compares unequal to 0

msam
  • 4,259
  • 3
  • 19
  • 32