Fortran Data Types Literal constants


Example

Program units often make use of literal constants. These cover the obvious cases like

print *, "Hello", 1, 1.0

Except in one case, each literal constant is a scalar which has type, type parameters and value given by the syntax.

Integer literal constants are of the form

1
-1
-1_1   ! For valid kind parameter 1
1_ik   ! For the named constant ik being a valid kind paramter

Real literal constants are of the form

1.0    ! Default real
1e0    ! Default real using exponent format
1._1   ! Real with kind parameter 1 (if valid)
1.0_sp ! Real with kind paramter named constant sp
1d0    ! Double precision real using exponent format
1e0_dp ! Real with kind named constant dp using exponent format

Complex literal constants are of the form

(1, 1.)       ! Complex with integer and real components, literal constants
(real, imag)  ! Complex with named constants as components

If the real and imaginary components are both integer, the complex literal constant is default complex, and the integer components are converted to default real. If one component is real, the kind parameter of the complex literal constant is that of the real (and the integer component is converted to that real kind). If both components are real the complex literal constant is of kind of the real with the greatest precision.

Logical literal constants are

.TRUE.     ! Default kind, with true value
.FALSE.    ! Default kind, with false value
.TRUE._1   ! Of kind 1 (if valid), with true value
.TRUE._lk  ! Of kind named constant lk (if valid), with true value

Character literal values differ slightly in concept, in that the kind specifier precedes the value

"Hello"       ! Character value of default kind
'Hello'       ! Character value of default kind
ck_"Hello"    ! Character value of kind ck
"'Bye"        ! Default kind character with a '
'''Bye'       ! Default kind character with a '
""            ! A zero-length character of default kind

As suggested above, character literal constants must be delimted by apostrophes or quotation marks, and the start and end marker must match. Literal apostrophes can be included by being within quotation mark delimiters or by appearing doubled. The same for quotation marks.

BOZ constants are distinct from the above, in that they specify only a value: they have no type or type parameter. A BOZ constant is a bit pattern and is specified as

B'00000'    ! A binary bit pattern
B"01010001" ! A binary bit pattern
O'012517'   ! An octal bit pattern
O"1267671"  ! An octal bit pattern
Z'0A4F'     ! A hexadecimal bit pattern
Z"FFFFFF"   ! A hexadecimal bit pattern

BOZ literal constants are limited in where they may appear: as constants in data statements and a selection of intrinsic procedures.