FORTRAN 77 Language Reference

Typeless Constants (Binary, Octal, Hexadecimal)

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 

Control Characters

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' )

Alternate Notation for Typeless Constants

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:

Hollerith 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.

Example: 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: