Next: , Previous: Implicitly convert LOGICAL and INTEGER values, Up: Extensions implemented in GNU Fortran

6.1.12 Hollerith constants support

GNU Fortran supports Hollerith constants in assignments, function arguments, and DATA and ASSIGN statements. A Hollerith constant is written as a string of characters preceded by an integer constant indicating the character count, and the letter H or h, and stored in bytewise fashion in a numeric (INTEGER, REAL, or complex) or LOGICAL variable. The constant will be padded or truncated to fit the size of the variable in which it is stored.

Examples of valid uses of Hollerith constants:

           complex*16 x(2)
           data x /16Habcdefghijklmnop, 16Hqrstuvwxyz012345/
           x(1) = 16HABCDEFGHIJKLMNOP
           call foo (4h abc)

Invalid Hollerith constants examples:

           integer*4 a
           a = 8H12345678 ! Valid, but the Hollerith constant will be truncated.
           a = 0H         ! At least one character is needed.

In general, Hollerith constants were used to provide a rudimentary facility for handling character strings in early Fortran compilers, prior to the introduction of CHARACTER variables in Fortran 77; in those cases, the standard-compliant equivalent is to convert the program to use proper character strings. On occasion, there may be a case where the intent is specifically to initialize a numeric variable with a given byte sequence. In these cases, the same result can be obtained by using the TRANSFER statement, as in this example.

           INTEGER(KIND=4) :: a
           a = TRANSFER ("abcd", a)     ! equivalent to: a = 4Habcd