Must be a single constant/literal, decimal (which in C automatically
means no leading* zero), integer (no decimal point or fractional
digits or exponent). (And no sign not even redundant plus.)
(* For the usual arithmetic meaning of 'leading', i.e. redundant, not
the stringwise meaning of contiguous to beginning. The number zero has
a single digit zero which is at the beginning but not redundant.)
static int powersOfTen[] =
{ 1
, 10
, 100
, 1000
, 10000
, 100000
, 1000000
, 10000000
, 100000000
,1000000000
};
Requires 32-bit (or at least 30-bit) int. long would be safer.
/* Returns the power of 10 with the same number of digits as X */
#define maxPowerOfTen(X) (powersOfTen[sizeof(#X)-2])
Doesn't work, eg. maxPowerOfTen(UINT_MAX) is always 7 because
#UINT_MAX is "UINT_MAX".
Even if you [indirect] might be [hex, etc.]
And even with these fixes(?) it doesn't produce something the C++
compiler considers a constant = compile-time expression, which is what
the PP wanted. (In C++ unlike C, a const int or enum (scalar)
initialized with a constant expression can be used, but still not an
array. Or for that matter function call, even if available and pure.)
- David.Thompson1 at worldnet.att.net