Numerical value of comparison result

Everywhere I look, it says zero is false and any other value is true. But I'm asking the opposite question. I assume the numerical value of false is zero, but what is the numerical value of true? If it's one, is it always one?

I want to set the alarm time on an RTC to the next multiple of five seconds, but at least one full second from now. So if it's currently 4 seconds, I want to set the alarm to 10 seconds, not 5.

  byte r = Sec % 5;             // alarm seconds multiple of 5
  Seconds = Sec +5 -r + (r==4)*5; // at least one second from now

That seems to work. But will any comparison that evaluates as true equal one? Always? (In C++ at least.)

Such a logic (comparison...) expression evaluates to 0 (false) or 1 (true) in C. Guaranteed! So your

definitely evaluates to either 0 or 5.

Do you have an official reference for that Guarantee?

BTW, Arduino is programmed in C++, not C.

Comparison operators - cppreference.com

Yea, just found it here:
https://en.cppreference.com/w/cpp/language/implicit_conversion#Integral_conversions:~:text=the%20type%20bool%20can,.

K&R

Not really a reference to the standard nor relevant to C++. The link provide by @mrburnette and the one in my subsequent post are.

Some definitions can not be changed in C++. Or would you like to check each new C/C++ version for eventual changes in the conditional values or other breaking news?

1 Like

You forgot:

  if (Seconds >= 60) {
    Seconds -= 60;
  }

Thanks very much. It's a settled issue then. So I can use it freely.

Actually @odometer, I didn't show it, but my version was:

if (Seconds > 55) Seconds = 0;

But that would miss 59. So I'll use your version.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.