[an error occurred while processing this directive]
HP OpenVMS Systems Documentation |
HP Fortran for OpenVMS
|
Previous | Contents | Index |
Binary, octal, hexadecimal, and Hollerith constants are nondecimal constants. They have no intrinsic data type, but assume a numeric data type depending on their use.
Fortran 95/90 allows unsigned binary, octal, and hexadecimal constants
to be used in DATA statements; the constant must correspond to an
integer scalar variable.
In HP Fortran, binary, octal, hexadecimal, and Hollerith constants
can appear wherever numeric constants are allowed.
3.4.1 Binary Constants
A binary constant is an alternative way to represent a numeric constant. A binary constant takes one of the following forms:
|
d
Is a binary (base 2) digit (0 or 1).
You can specify up to 256 binary digits in a binary constant. Leading zeros are ignored.
The following examples demonstrate valid and invalid binary constants:
Valid | |
B'0101110' | |
B"1" | |
Invalid | Explanation |
B'0112' | The character 2 is invalid. |
B10011' | No apostrophe after the B. |
"1000001" | No B before the first quotation mark. |
An octal constant is an alternative way to represent numeric constants. An octal constant takes one of the following forms:
|
d
Is an octal (base 8) digit (0 through 7).
You can specify up to 256 bits in octal (86 octal digits) constants. Leading zeros are ignored.
The following examples demonstrate valid and invalid octal constants:
Valid | |
O'07737' | |
O"1" | |
Invalid | Explanation |
O'7782' | The character 8 is invalid. |
O7772' | No apostrophe after the O. |
"0737" | No O before the first quotation mark. |
On an alternative form for octal constants, see Section B.7.
3.4.3 Hexadecimal Constants
A hexadecimal constant is an alternative way to represent numeric constants. A hexadecimal constant takes one of the following forms:
|
d
Is a hexadecimal (base 16) digit (0 through 9, or an uppercase or lowercase letter in the range of A to F).
You can specify up to 256 bits in hexadecimal (64 hexadecimal digits) constants. Leading zeros are ignored.
The following examples demonstrate valid and invalid hexadecimal constants:
Valid | |
Z'AF9730' | |
Z"FFABC" | |
Z'84' | |
Invalid | Explanation |
Z'999.' | Decimal not allowed. |
ZF9" | No quotation mark after the Z. |
On an alternative form for hexadecimal constants, see Section B.7.
A Hollerith constant is a string of printable ASCII
characters preceded by the letter H. Before the H, there must be an
unsigned, nonzero default integer constant stating the number of
characters in the string (including blanks and tabs).
Hollerith constants are strings of 1 to 2000 characters. They are
stored as byte strings, one character per byte.
The following examples demonstrate valid and invalid Hollerith
constants:
3.4.4 Hollerith Constants
Valid
16HTODAY'S DATE IS:
1HB
4H ABC
Invalid
Explanation
3HABCD
Wrong number of characters.
0H
Hollerith constants must contain at least one character.
3.4.5 Determining the Data Type of Nondecimal Constants
Binary, octal, hexadecimal, and Hollerith constants have no intrinsic data type. These constants assume a numeric data type depending on their use.
When the constant is used with a binary operator (including the assignment operator), the data type of the constant is the data type of the other operand. For example:
Statement | Data Type of Constant |
Length of Constant (in bytes) |
---|---|---|
INTEGER(2) ICOUNT | ||
INTEGER(4) JCOUNT | ||
INTEGER(4) N | ||
REAL(8) DOUBLE | ||
REAL(4) RAFFIA, RALPHA | ||
RAFFIA = B'1001100111111010011' | REAL(4) | 4 |
RAFFIA = Z'99AF2' | REAL(4) | 4 |
RALPHA = 4HABCD | REAL(4) | 4 |
DOUBLE = B'1111111111100110011010' | REAL(8) | 8 |
DOUBLE = Z'FFF99A' | REAL(8) | 8 |
DOUBLE = 8HABCDEFGH | REAL(8) | 8 |
JCOUNT = ICOUNT + B'011101110111' | INTEGER(2) | 2 |
JCOUNT = ICOUNT + O'777' | INTEGER(2) | 2 |
JCOUNT = ICOUNT + 2HXY | INTEGER(2) | 2 |
IF (N .EQ. B'1010100') GO TO 10 | INTEGER(4) | 4 |
IF (N .EQ. O'123') GO TO 10 | INTEGER(4) | 4 |
IF (N. EQ. 1HZ) GO TO 10 | INTEGER(4) | 4 |
When a specific data type (generally integer) is required, that type is assumed for the constant. For example:
Statement | Data Type of Constant |
Length of Constant (in bytes) |
---|---|---|
Y(IX) = Y(O'15') + 3. | INTEGER(4) | 4 |
Y(IX) = Y(1HA) + 3. | INTEGER(4) | 4 |
When a nondecimal constant is used as an actual argument, the following occurs:
For example:
Statement | Data Type of Constant |
Length of Constant (in bytes) |
---|---|---|
CALL APAC(Z'34BC2') | INTEGER(8) | 8 |
CALL APAC(9HABCDEFGHI) | None | 9 |
When a binary, octal, or hexadecimal constant is used in any other context, the default integer data type is assumed (default integer can be affected by compiler options). In the following examples, default integer is INTEGER(4):
Statement | Data Type of Constant |
Length of Constant (in bytes) |
---|---|---|
IF (Z'AF77') 1,2,3 | INTEGER(4) | 4 |
IF (2HAB) 1,2,3 | INTEGER(4) | 4 |
I = O'7777' - Z'A39' 1 | INTEGER(4) | 4 |
I = 1HC - 1HA | INTEGER(4) | 4 |
J = .NOT. O'73777' | INTEGER(4) | 4 |
J = .NOT. 1HB | INTEGER(4) | 4 |
When nondecimal constants are not the same length as the length implied by a data type, the following occurs:
On compiler options, see the HP Fortran for OpenVMS User Manual.
3.5 Variables
A variable is a data object whose value can be changed at any point in a program. A variable can be any of the following:
An array element
An array section
A structure component
A character substring
The name of a variable is associated with a single storage location.
Variables are classified by data type, as constants are. The data type of a variable indicates the type of data it contains, including its precision, and implies its storage requirements. When data of any type is assigned to a variable, it is converted to the data type of the variable (if necessary).
A variable is defined when you give it a value. A variable can be defined before program execution by a DATA statement or a type declaration statement. During program execution, variables can be defined or redefined in assignment statements and input statements, or undefined (for example, if an I/O error occurs). When a variable is undefined, its value is unpredictable.
When a variable becomes undefined, all variables associated by storage association also become undefined.
The data type of a scalar variable can be explicitly declared in a type declaration statement. If no type is declared, the variable has an implicit data type based on predefined typing rules or definitions in an IMPLICIT statement.
An explicit declaration of data type takes precedence over any implicit
type. Implicit type specified in an IMPLICIT statement takes precedence
over predefined typing rules.
3.5.1.1 Specification of Data Type
Type declaration statements explicitly specify the data type of scalar variables. For example, the following statements associate VAR1 with an 8-byte complex storage location, and VAR2 with an 8-byte double-precision storage location:
COMPLEX VAR1 DOUBLE PRECISION VAR2 |
You can explicitly specify the data type of a scalar variable only once.
If no explicit data type specification appears, any variable with a name that begins with the letter in the range specified in the IMPLICIT statement becomes the data type of the variable.
Character type declaration statements specify that given variables represent character values with the length specified. For example, the following statements associate the variable names INLINE, NAME, and NUMBER with storage locations containing character data of lengths 72, 12, and 9, respectively:
CHARACTER*72 INLINE CHARACTER NAME*12, NUMBER*9 |
In single subprograms, assumed-length character arguments can be used to process character strings with different lengths. The assumed-length character argument has its length specified with an asterisk, for example:
CHARACTER*(*) CHARDUMMY |
The argument CHARDUMMY assumes the length of the actual argument.
By default, all scalar variables with names beginning with I, J, K, L, M, or N are assumed to be default integer variables. Scalar variables with names beginning with any other letter are assumed to be default real variables. For example:
Real Variables | Integer Variables |
ALPHA | JCOUNT |
BETA | ITEM_1 |
TOTAL_NUM | NTOTAL |
Names beginning with a dollar sign ($) are implicitly INTEGER.
You can override the default data type implied in a name by specifying data type in either an IMPLICIT statement or a type declaration statement.
An array is a set of scalar elements that have the same type and kind parameters. Any object that is declared with an array specification is an array. Arrays can be declared by using a type declaration statement, or by using a DIMENSION, COMMON, ALLOCATABLE, POINTER, or TARGET statement.
An array can be referenced by element (using subscripts), by section (using a section subscript list), or as a whole. A subscript list (appended to the array name) indicates which array element or array section is being referenced.
A section subscript list consists of subscripts, subscript triplets, or vector subscripts. At least one subscript in the list must be a subscript triplet or vector subscript.
When an array name without any subscripts appears in an intrinsic operation (for example, addition), the operation applies to the whole array (all elements in the array).
An array has the following properties:
The name and rank of an array must be specified when the array is declared. The extent of each dimension can be constant, but does not need to be. The extents can vary during program execution if the array is a dummy argument array, an automatic array, an array pointer, or an allocatable array.
A whole array is referenced by the array name. Individual elements in a named array are referenced by a scalar subscript or list of scalar subscripts (if there is more than one dimension). A section of a named array is referenced by a section subscript.
The following are examples of valid array declarations:
DIMENSION A(10, 2, 3) ! DIMENSION statement ALLOCATABLE B(:, :) ! ALLOCATABLE statement POINTER C(:, :, :) ! POINTER statement REAL, DIMENSION (2, 5) ! Type declaration with ! DIMENSION attribute |
Consider the following array declaration:
INTEGER L(2:11,3) |
The properties of array L are as follows:
Data type: | INTEGER |
Rank: | 2 (two dimensions) |
Bounds: | First dimension: 2 to 11 |
Second dimension: 1 to 3 | |
Size: | 30; the product of the extents: 10 x 3 |
Shape: | (/10,3/) (or 10 by 3); a vector of the extents 10 and 3 |
The following example shows other valid ways to declare this array:
DIMENSION L(2:11,3) INTEGER, DIMENSION(2:11,3) :: L COMMON L(2:11,3) |
The following example shows references to array elements, array sections, and a whole array:
REAL B(10) ! Declares a rank-one array with 10 elements INTEGER C(5,8) ! Declares a rank-two array with 5 elements in ! dimension one and 8 elements in dimension two ... B(3) = 5.0 ! Reference to an array element B(2:5) = 1.0 ! Reference to an array section consisting of ! elements: B(2), B(3), B(4), B(5) ... C(4,8) = I ! Reference to an array element C(1:3,3:4) = J ! Reference to an array section consisting of ! elements: C(1,3) C(1,4) ! C(2,3) C(2,4) ! C(3,3) C(3,4) B = 99 ! Reference to a whole array consisting of ! elements: B(1), B(2), B(3), B(4), B(5), ! B(6), B(7), B(8), B(9), and B(10) |
Previous | Next | Contents | Index |