Floating point literals

Floating point literals are numbers that have a decimal point or an exponential part. They can be represented as:

Binary floating point literals

A real binary floating point constant consists of the following:

Both the integral and fractional parts are made up of decimal digits. You can omit either the integral part or the fractional part, but not both. You can omit either the decimal point or the exponent part, but not both.

Read syntax diagramSkip visual syntax diagram
Binary floating point literal syntax

     .-----------.     .-------.                          
     V           |     V       |                          
>>-+---+-------+-+--.----digit-+--| exponent |-+--+---+--------><
   |   '-digit-'                               |  +-f-+   
   | .-------.                                 |  +-F-+   
   | V       |                                 |  +-l-+   
   +---digit-+--.--| exponent |----------------+  '-L-'   
   | .-------.                                 |          
   | V       |                                 |          
   '---digit-+--| exponent |-------------------'          

Exponent

                  .-------.   
                  V       |   
|--+-e-+--+----+----digit-+-------------------------------------|
   '-E-'  +-+--+              
          '- --'              

The suffix f or F indicates a type of float, and the suffix l or L indicates a type of long double. If a suffix is not specified, the floating point constant has a type double.

A plus (+) or minus (-) symbol can precede a floating point literal. However, it is not part of the literal; it is interpreted as a unary operator.

The following are examples of floating point literals:

Floating point constant Value
5.3876e4 53,876
4e-11 0.00000000004
1e+5 100000
7.321E-3 0.007321
3.2E+4 32000
0.5e-6 0.0000005
0.45 0.45
6.e10 60000000000

Hexadecimal floating point literals

Real hexadecimal floating constants, which are a C99 feature, consist of the following:

The significant part represents a rational number and is composed of the following:

The optional fraction part is a period followed by a sequence of hexadecimal digits.

The exponent part indicates the power of 2 to which the significant part is raised, and is an optionally signed decimal integer. The type suffix is optional. The full syntax is as follows:

Read syntax diagramSkip visual syntax diagram
Hexadecimal floating point literal syntax

>>-+-0x-+------------------------------------------------------->
   '-0X-'   

     .------------------.     .------------------.                   
     V                  |     V                  |                   
>--+---+--------------+-+--.----+-digit_0_to_f-+-+--| exponent |-+-->
   |   +-digit_0_to_f-+         '-digit_0_to_F-'                 |   
   |   '-digit_0_to_F-'                                          |   
   | .------------------.                                        |   
   | V                  |                                        |   
   +---+-digit_0_to_f-+-+--.--| exponent |-----------------------+   
   |   '-digit_0_to_F-'                                          |   
   | .------------------.                                        |   
   | V                  |                                        |   
   '---+-digit_0_to_f-+-+--| exponent |--------------------------'   
       '-digit_0_to_F-'                                              

>--+---+-------------------------------------------------------><
   +-f-+   
   +-F-+   
   +-l-+   
   '-L-'   

Exponent

                  .--------------.   
                  V              |   
|--+-p-+--+----+----digit_0_to_9-+------------------------------|
   '-P-'  +-+--+                     
          '- --'                     

The suffix f or F indicates a type of float, and the suffix l or L indicates a type of long double. If a suffix is not specified, the floating point constant has a type double. You can omit either the whole-number part or the fraction part, but not both. The binary exponent part is required to avoid the ambiguity of the type suffix F being mistaken for a hexadecimal digit.

Complex literals

Complex literals, which are a C99 feature, are constructed in two parts: the real part, and the imaginary part.

Read syntax diagramSkip visual syntax diagram
Complex literal syntax

>>-| real part |--+-------+--| imaginary part |----------------->
                  '-+-- –-'                       

>--| Imaginary part |--floating point constant--*--_Complex_I--><

Real part

|--floating point constant--------------------------------------|

The floating point constant can be specified as a decimal or hexadecimal floating point constant (including optional suffixes), in any of the formats described in the previous sections.

_Complex_I is a macro defined in the complex.h header file, representing the imaginary unit i, the square root of -1.

For example, the declaration:
varComplex = 2.0f + 2.0f * _Complex_I;
initializes the complex variable varComplex to a value of 2.0 + 2.0i.

Begin IBM extension For ease of porting applications developed with GNU C, XL C/C++ also allows you to indicate the imaginary part of a complex literal with a suffix, in addition to the standard suffixes that indicate the type of the complex number (float, double, or long double).

The simplified syntax for a complex literal using the GNU suffixes is as follows:
Read syntax diagramSkip visual syntax diagram
>>-| real part |--+-------+--| imaginary part |----------------><
                  '-+-- –-'                       

real part

|----floating point constant------------------------------------|

imaginary part

|--floating point constant--imaginary-suffix--------------------|

floating point constant can be specified as a decimal or hexadecimal floating point constant (including optional suffixes), in any of the formats described in the previous sections.

imaginary-suffix is one of the suffixes i, I, j, or J, representing the imaginary unit.

For example, the declaration
varComplex = 3.0f + 4.0fi;
initializes the complex variable varComplex to a value of 3.0 + 4.0i. End IBM extension