In an application I'm working on, we have a macro to convert clock times into an internal representation as a short integer:
#define CLOCK_TIME(hr, min) (s32)(((hr) * 60 + (min)) * 0x10000 / (24 * 60))
Naturally, we would write the hours and minutes exactly as they'd appear on a clock. So, for example, 6:30 would be CLOCK_TIME(6, 30). This all worked fine until someone tried to enter CLOCK_TIME(6, 08) and the compiler spat out an error saying 08 is not a valid octal constant. We tried a few workarounds, but they led to their own problems. For example, replacing min with 1##min - 100 fixed that specific issue, but then the macro would fail with any input that wasn't a 2-digit decimal literal. Is there a way we could allow 08 and 09 to work in the macro while also allowing arguments that may be single digits, expressions, or variables?
And yes, I realize just not using leading zeroes is a solution, but having the times appear the way they appear on a clock improves both readability and usability.
EDIT: One additional constraint is that if the time is a constant, it must be calculated at compile time and not runtime.
As @dimich suggested, you can append scientific notation
e0.This will first force the number to be a double, then you can type-cast it back to an
int.This works because
08e0is valid as(double)8.000, even though08is invalid octal.IDEOne Link