In C, if I want a floating point literal x to be in hexadecimal form and have an exponential value, it will be denoted as this:
float x = 0X2aE+10;
Where the 0x is used to denote it's in hexadecimal, and the 2a for the hexadecimal characters, and the E+10 for the number into ten to the power ten.
However, will the compiler know it's not 2aE that's representing the hexadecimal digits (since hexadecimal uses a to e for 10-15) and +10 is just adding ten to the number? And if it doesn't, what's the fix?
0X2aE+10
is a valid preprocessor token that does not translate into a valid C token, hence the compilation error. C99 Hexadecimal floating point numbers use P
or p
as the exponent marker, as in:
float x = 0X2aP+10;
Which has the value 0x2a * 1024
-> 43008
.
For those who do not know about preprocessing numbers:
6.4.8 Preprocessing numbers
Syntax
pp-number:
digit
. digit
pp-number digit
pp-number identifier-nondigit
pp-number e sign
pp-number E sign
pp-number p sign
pp-number P sign
pp-number .
Description
A preprocessing number begins with a digit optionally preceded by a period (.) and may be followed by valid identifier characters and the character sequences e+
, e-
, E+
, E-
, p+
, p-
, P+
, or P-
.
Preprocessing number tokens lexically include all floating and integer constant tokens.
Semantics
A preprocessing number does not have type or a value; it acquires both after a successful conversion (as part of translation phase 7) to a floating constant token or an integer constant token.
Hex floats use p
as the exponent marker:
float x = 0x2ap+10;
That's actually a double
value, though. If you want a float
literal, you need an f
suffix:
float x = 0x2ap+10f;
If you see e.g. this floating point literal reference you will see that hexadecimal floating point values need the p
or P
infix to denote the exponent.
So the definition should look like
float x = 0X2ap10;