Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the 'E/e' in hexadecimal differentiated from the 'E/e' in exponential form in a hexadecimal floating point literal?

Tags:

c

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?

like image 261
Aryan poonacha Avatar asked Jul 16 '16 15:07

Aryan poonacha


1 Answers

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.

like image 198
chqrlie Avatar answered Sep 21 '22 05:09

chqrlie