Next: , Previous: , Up: Extensions implemented in GNU Fortran   [Contents][Index]


6.1.14 Hollerith constants support

GNU Fortran supports Hollerith constants in assignments, DATA statements, function and subroutine arguments. 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), LOGICAL or CHARACTER variable. The constant will be padded with spaces 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)

Examples of Hollerith constants:

      integer*4 a
      a = 0H         ! Invalid, at least one character is needed.
      a = 4HAB12     ! Valid
      a = 8H12345678 ! Valid, but the Hollerith constant will be truncated.
      a = 3Hxyz      ! Valid, but the Hollerith constant will be padded.

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

The use of the -fdec option extends support of Hollerith constants to comparisons:

      integer*4 a
      a = 4hABCD
      if (a .ne. 4habcd) then
        write(*,*) "no match"
      end if

Supported types are numeric (INTEGER, REAL, or COMPLEX), and CHARACTER.


Next: , Previous: , Up: Extensions implemented in GNU Fortran   [Contents][Index]