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       |                              
>>-+-+-----------+--.----digit-+--+--------------+-+--+---+----><
   | | .-------. |                '-| exponent |-' |  +-f-+   
   | | V       | |                                 |  +-F-+   
   | '---digit-+-'                                 |  +-l-+   
   | .-------.                                     |  '-L-'   
   | V       |                                     |          
   +---digit-+--.--+--------------+----------------+          
   |               '-| exponent |-'                |          
   | .-------.                                     |          
   | 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 (C only)

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

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 were introduced in the C99 standard, 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 |-------------------><
                  '- –-'                       

Real part

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

Imaginary part

|--floating-point constant--*--_Complex_I-----------------------|

floating-point constant can be specified as a hexadecimal floating-point literal (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.