Typeless numeric constants are so named because their expressions assume data types based on how they are used. @
These constants are not converted before use. However, in f77, they must be distinguished from character strings.
The general form is to enclose a string of appropriate digits in apostrophes and prefix it with the letter B, O, X, or Z. The B is for binary, the O is for octal, and the X or Z are for hexadecimal.
Example: Binary, octal, and hexadecimal constants, DATA and PARAMETER:
PARAMETER ( P1 = Z'1F' ) INTEGER*2 N1, N2, N3, N4 DATA N1 /B'0011111'/, N2/O'37'/, N3/X'1f'/, N4/Z'1f'/ WRITE ( *, 1 ) N1, N2, N3, N4, P1 1 FORMAT ( 1X, O4, O4, Z4, Z4, Z4 ) END
Note the edit descriptors in FORMAT statements: O for octal, and Z for hexadecimal. Each of the above integer constants has the value 31 decimal.
Example: Binary, octal, and hexadecimal, other than in DATA and PARAMETER:
INTEGER*4 M, ICOUNT/1/, JCOUNT REAL*4 TEMP M = ICOUNT + B'0001000' JCOUNT = ICOUNT + O'777' TEMP = X'FFF99A' WRITE(*,*) M, JCOUNT, TEMP END
In the above example, the context defines B'0001000' and O'777' as INTEGER*4 and X'FFF99A' as REAL*4. For a real number, using IEEE floating-point, a given bit pattern yields the same value on different architectures.
The above statements are treated as the following:
M = ICOUNT + 8 JCOUNT = ICOUNT + 511 TEMP = 2.35076E-38
You can enter control characters with typeless constants, although the CHAR function is standard, and this way is not.
Example: Control characters with typeless constants:
CHARACTER BELL, ETX / X'03' / PARAMETER ( BELL = X'07' )
For compatibility with other versions of FORTRAN, the following alternate notation is allowed for octal and hexadecimal notation. This alternate does not work for binary, nor does it work in DATA or PARAMETER statements.
For an octal notation, enclose a string of octal digits in apostrophes and append the letter O.
Example: Octal alternate notation for typeless constants:
'37'O 37'O Invalid -- missing initial apostrophe '37' Not numeric -- missing letter O '397'O Invalid -- invalid digit
For hexadecimals, enclose a string of hex digits in apostrophes and append the letter X.
Example: Hex alternate notation for typeless constants:
'ab'X 3fff'X '1f'X '1fX Invalid-missing trailing apostrophe '3f' Not numeric- missing X '3g7'X Invalid-invalid digit g
Here are the rules and restrictions for binary, octal, and hexadecimal constants:
These constants are for use anywhere numeric constants are allowed.
These constants are typeless. They are stored in the variables without any conversion to match the type of the variable, but they are stored in the appropriate part of the receiving field--low end, high end.
If the receiving data type has more digits than are specified in the constant, zeros are filled on the left.
If the receiving data type has fewer digits than are specified in the constant, digits are truncated on the left. If nonzero digits are lost, an error message is displayed.
Specified leading zeros are ignored.
You can specify up to 8 bytes of data for any one constant--at least that's all that are used.
If a typeless constant is an actual argument, it has no data type, but it is always 4 bytes that are passed.
For binary constants, each digit must be 0 or 1.
For octal constants, each digit must be in the range 0 to 7.
For hexadecimal constants, each digit must be in the range 0 to 9 or in the range A to F, or a to f.
Outside of DATA statements, such constants are treated as the type required by the context. If a typeless constant is used with a binary operator, it gets the data type of the other operand (8.0 + '37'O).
In DATA statements, such constants are treated as typeless binary, hexadecimal, or octal constants.
A Hollerith constant consists of an unsigned, nonzero, integer constant, followed by the letter H, followed by a string of printable characters where the integer constant designates the number of characters in the string, including any spaces and tabs.
A Hollerith constant occupies 1 byte of storage for each character.
A Hollerith constant is aligned on 2-byte boundaries.
The FORTRAN standard does not have this old Hollerith notation, although the standard recommends implementing the Hollerith feature to improve compatibility with old programs.
Hollerith data can be used in place of character-string constants. They can also be used in IF tests, and to initialize noncharacter variables in DATA statements and assignment statements, though none of these are recommended, and none are standard. These are typeless constants.
CHARACTER C*1, CODE*2 INTEGER TAG*2 DATA TAG / 2Hok / CODE = 2Hno IF ( C .EQ. 1HZ ) CALL PUNT
The rules and restrictions on Hollerith constants are:
The number of characters has no practical limit.
The characters can continue over to a continuation line, but that gets tricky. Short standard fixed format lines are padded on the right with blanks up to 72 columns, but short tab-format lines stop at the newline.
If a Hollerith constant is used with a binary operator, it gets the data type of the other operand.
If you assign a Hollerith constant to a variable, and the length of the constant is less than the length of the data type of the variable, then spaces (ASCII 32) are appended on the right.
If the length of a Hollerith constant or variable is greater than the length of the data type of the variable, then characters are truncated on the right.
If a Hollerith constant is used as an actual argument, it is passed as a 4-byte item.
If a Hollerith constant is used, and the context does not determine the data type, then INTEGER*4 is used.