BOZ literal constants - The GNU Fortran Compiler
Description: The GNU Fortran Compiler

Next: Real array indices, Previous: <code>Q</code> exponent-letter, Up: Extensions implemented in GNU Fortran

#### 6.1.9 BOZ literal constants

Besides decimal constants, Fortran also supports binary (`b`), octal (`o`) and hexadecimal (`z`) integer constants. The syntax is: ‘`prefix quote digits quote`’, were the prefix is either `b`, `o` or `z`, quote is either `'` or `"` and the digits are for binary `0` or `1`, for octal between `0` and `7`, and for hexadecimal between `0` and `F`. (Example: `b'01011101'`.)

Up to Fortran 95, BOZ literals were only allowed to initialize integer variables in DATA statements. Since Fortran 2003 BOZ literals are also allowed as argument of `REAL`, `DBLE`, `INT` and `CMPLX`; the result is the same as if the integer BOZ literal had been converted by `TRANSFER` to, respectively, `real`, `double precision`, `integer` or `complex`. As GNU Fortran extension the intrinsic procedures `FLOAT`, `DFLOAT`, `COMPLEX` and `DCMPLX` are treated alike.

As an extension, GNU Fortran allows hexadecimal BOZ literal constants to be specified using the `X` prefix, in addition to the standard `Z` prefix. The BOZ literal can also be specified by adding a suffix to the string, for example, `Z'ABC'` and `'ABC'Z` are equivalent.

Furthermore, GNU Fortran allows using BOZ literal constants outside DATA statements and the four intrinsic functions allowed by Fortran 2003. In DATA statements, in direct assignments, where the right-hand side only contains a BOZ literal constant, and for old-style initializers of the form `integer i /o'0173'/`, the constant is transferred as if `TRANSFER` had been used; for `COMPLEX` numbers, only the real part is initialized unless `CMPLX` is used. In all other cases, the BOZ literal constant is converted to an `INTEGER` value with the largest decimal representation. This value is then converted numerically to the type and kind of the variable in question. (For instance, `real :: r = b'0000001' + 1` initializes `r` with `2.0`.) As different compilers implement the extension differently, one should be careful when doing bitwise initialization of non-integer variables.

Note that initializing an `INTEGER` variable with a statement such as `DATA i/Z'FFFFFFFF'/` will give an integer overflow error rather than the desired result of -1 when `i` is a 32-bit integer on a system that supports 64-bit integers. The ‘`-fno-range-check`’ option can be used as a workaround for legacy code that initializes integers in this manner.