# Floating constant

< c‎ | language

C
 Language Headers Type support Program utilities Variadic function support Error handling Dynamic memory management Date and time utilities Strings library Algorithms Numerics Input/output support Localization support Atomic operations (C11) Thread support (C11) Technical Specifications

C language
 Basic concepts Keywords Preprocessor Statements Expressions Initialization Declarations Functions Miscellaneous History of C Technical Specifications

Allows values of floating type to be used directly in expressions.

## Contents

### Syntax

A floating constant is a non-lvalue expression having the form:

 significand exponent(optional) suffix(optional)

Where the significand has the form

 whole-number(optional) .(optional) fraction(optional)

The exponent has the form

 e | E exponent-sign(optional) digit-sequence (1) p | P exponent-sign(optional) digit-sequence (2) (since C99)
1) The exponent syntax for a decimal floating-point constant
2) The exponent syntax for hexadecimal floating-point constant

### Explanation

 If the significand begins with the character sequence 0x or 0X, the floating constant is a hexadecimal floating constant. Otherwise, it is a decimal floating constant. For a hexadecimal floating constant, the significand is interpreted as a hexadecimal rational number, and the digit-sequence of the exponent is interpreted as the integer power of 2 to which the significand has to be scaled. double d = 0x1.2p3; // hex fraction 1.2 (decimal 1.125) scaled by 2^3, that is 9.0 (since C99)

For a decimal floating constant, the significand is interpreted as a decimal rational number, and the digit-sequence of the exponent is interpreted as the integer power of 10 to which the significand has to be scaled.

double d = 1.2e3; // decimal fraction 1.2 scaled by 10^3, that is 1200.0

#### Suffixes

An unsuffixed floating constant has type double. If suffix is the letter f or F, the floating constant has type float. If suffix is the letter l or L, the floating constant has type long double.

 If the implementation predefines macro __STDC_IEC_60559_BFP__, the following suffixes and corresponding floating constants are additionally supported: if suffix is df or DF, the floating constant has type _Decimal32; if suffix is dd or DD, the floating constant has type _Decimal64; if suffix is dl or DL, the floating constant has type _Decimal128. Suffixes for decimal floating-point types are not allowed in hexadecimal floating constants. (since C23)

#### Optional parts

If the exponent is present and fractional part is not used, the decimal separator may be omitted:

double x = 1e0; // floating-point 1.0 (period not used)

For decimal floating constants, the exponent part is optional. If it is omitted, the period is not optional, and either the whole-number or the fraction must be present.

double x = 1.; // floating-point 1.0 (fractional part optional)
double y = .1; // floating-point 0.1 (whole-number part optional)
 For hexadecimal floating constants, the exponent is not optional to avoid ambiguity resulting from an f suffix being mistaken as a hexadecimal digit. (since C99)

#### Representable values

The result of evaluating a floating constant is either the nearest representable value or the larger or smaller representable value immediately adjacent to the nearest representable value, chosen in an implementation-defined manner (in other words, default rounding direction during translation is implementation-defined).

All floating constants of the same source form convert to the same internal format with the same value. Floating constants of different source forms, e.g. 1.23 and 1.230, need not to convert to the same internal format and value.

 Floating-point constants may convert to more range and precision than is indicated by their type, if indicated by FLT_EVAL_METHOD. For example, the constant 0.1f may act as if it were 0.1L in an expression. The result of evaluating a hexadecimal floating constant, if FLT_RADIX is 2, is the exact value represented by the floating constant, correctly rounded to the target type. (since C99)
 Floating constants of decimal floating-point type that have the same numerical value x but different quantum exponents, e.g. 1230.dd, 1230.0dd, and 1.23e3dd, have distinguishable internal representations. The quantum exponent q of a floating constant of a decimal floating-point type is determined in the manner that 10q represents 1 at the place of last digit of the significand when possible. If the quantum exponent q and the coefficient c=x·10-q determined above cannot represented exactly in the type of the floating constant, q is increased as needed within the limit of the type, and c is reduced correspondingly, with needed rounding. The rounding may result in zero or infinity. If (the possibly rounded) c is still out of the permitted range after q reaches the maximum value, the resulted floating constant has value positive infinity. (since C23)