A literal constant is a datum whose value cannot change throughout the program unit. The form of the string representing a constant determines the value and data type of the constant. (For a named constant, defined by a PARAMETER statement, the name defines the data type.)
There are three general kinds of constants:
Arithmetic
Logical
Character
Blank characters within an arithmetic or logical constant do not affect the value of the constant. Within character constants, they do affect the value.
Here are the different kinds of arithmetic constants:
Typed Constants |
Typeless Constants |
---|---|
Complex |
Binary |
Double complex |
Octal |
Double precision |
Hexadecimal |
Integer |
Hollerith |
Real |
|
A signed constant is an arithmetic constant with a leading plus or minus sign. An unsigned constant is an arithmetic constant without a leading sign.
For integer, real, and double-precision data, zero is neither positive nor negative. The value of a signed zero is the same as that of an unsigned zero.
Compiling with any of the options -i2, -dbl, -r8, or -xtypemap alters the default size of integer, real, complex, and double precision constants. These options are described in Chapter 2, and in the Fortran User's Guide.
A character-string constant is a string of characters enclosed in apostrophes or quotes. The apostrophes are standard; the quotes are not. @
If you compile with the -xl option, then the quotes mean something else, and you must use apostrophes to enclose a string.
To include an apostrophe in an apostrophe-delimited string, repeat it. To include a quote in a quote-delimited string, repeat it. Examples:
'abc' "abc" 'ain''t' "in vi type ""h9Y"
If a string begins with one kind of delimiter, the other kind can be embedded within it without using the repeated quote or backslash escapes. See Table 2-3.
"abc" "abc" "ain't" 'in vi type "h9Y'
Each character string constant appearing outside a DATA statement is followed by a null character to ease communication with C routines. You can make character string constants consisting of no characters, but only as arguments being passed to a subprogram. Such zero length character string constants are not FORTRAN standard.
Example: Null character string:
demo% cat NulChr.f write(*,*) 'a', '', 'b' stop end demo% f77 NulChr.f NulChr.f: MAIN: demo% a.out ab demo%
However, if you put such a null character constant into a character variable, the variable will contain a blank, and have a length of at least 1 byte.
Example: Length of null character string:
demo% cat NulVar.f character*1 x / 'a' /, y / '' /, z / 'c' / write(*,*) x, y, z write(*,*) len( y ) end demo% f77 NulVar.f NulVar.f: MAIN: demo% a.out a c 1 demo%
For compatibility with C usage, the following backslash escapes are recognized. If you include the escape sequence in a character string, then you get the indicated character.
Table 2-3 Backslash Escape Sequences
Escape Sequence |
Character |
---|---|
\n |
Newline |
\r |
Carriage return |
\t |
Tab |
\b |
Backspace |
\f |
Form feed |
\v |
Vertical tab |
\0 |
Null |
\' |
Apostrophe, which does not terminate a string |
\" |
Quotation mark, which does not terminate a string |
\\ |
\ |
\x |
x, where x is any other character |
If you compile with the -xl option, then the backslash character (\) is treated as an ordinary character. That is, with the -xl option, you cannot use these escape sequences to get special characters.
Technically, the escape sequences are not nonstandard, but are implementation- defined.
A complex constant is an ordered pair of real or integer constants (or PARAMETER constants@). The constants are separated by a comma, and the pair is enclosed in parentheses. The first constant is the real part, and the second is the imaginary part. A complex constant, COMPLEX*8, uses 8 bytes of storage.
( 9.01, .603 ) ( +1.0, -2.0 ) ( +1.0, -2 ) ( 1, 2 ) ( 4.51, ) Invalid -need second part
A double-complex constant, COMPLEX*16, is an ordered pair of real or integer constants, where one of the constants is REAL*8, and the other is INTEGER, REAL*4, or REAL*8. @
The constants are separated by a comma, and the pair is enclosed in parentheses. The first constant is the real part, and the second is the imaginary part. A double-complex constant, COMPLEX*16, uses 16 bytes of storage.
Example: Double-complex constants:
( 9.01D6, .603 ) ( +1.0, -2.0D0 ) ( 1D0, 2 ) ( 4.51D6, ) Invalid-need second part ( +1.0, -2.0 ) Not DOUBLE COMPLEX-need a REAL*8 |
(SPARC only) A quad complex constant @ is an ordered pair of real or integer constants, where one of the constants is REAL*16, and the other is INTEGER, REAL*4, REAL*8, or REAL*16. @
The constants are separated by a comma, and the pair is enclosed in parentheses. The first constant is the real part, and the second is the imaginary part. A quad complex constant, COMPLEX*32 @, uses 32 bytes of storage.
Example: Quad complex constants (SPARC only):
( 9.01Q6, .603 ) ( +1.0, -2.0Q0 ) ( 1Q0, 2 ) ( 3.3Q-4932, 9 ) ( 1, 1.1Q+4932 ) ( 4.51Q6, ) Invalid-need second part ( +1.0, -2.0 ) Not quad complex -need a REAL*16
An integer constant consists of an optional plus or minus sign, followed by a string of decimal digits.
Restrictions:
No other characters are allowed except, of course, a space.
If no sign is present, the constant is assumed to be nonnegative.
The value must be in the INTEGER*4 range (-2147483648, 2147483647), unless compiled with an option that promotes integers to 64 bits, in which case the range becomes INTEGER*8 (-9223372036854775808,9223372036854775807). See "Size and Alignment of Data Types ".
-2147483648 -2147483649 Invalid-too small, error message -10 0 +199 29002 2.71828 Not INTEGER-decimal point not allowed 1E6 Not INTEGER-E not allowed 29,002 Invalid-comma not allowed, error message 2147483647 2147483648 Invalid- too large, error message
You can also specify integer constants with the following alternate octal notation. Precede an integer string with a double quote (") and compile with the -xl option. These are octal constants of type INTEGER.
Example: The following two statements are equivalent:
JCOUNT = ICOUNT + "703 JCOUNT = ICOUNT + 451
You can also specify typeless constants as binary, octal, hexadecimal, or Hollerith. See "Typeless Constants (Binary, Octal, Hexadecimal) ".
Compiling with an option that promotes the range from INTEGER*4(-21474836, 21474836) to INTEGER*8 (-9223372036854775808, 9223372036854775807). The integer constant is stored or passed as an 8-byte integer, data type INTEGER*8.
If a constant argument is in the range (-32768, 32767), it is usually widened to a 4-byte integer, data type INTEGER*4; but compiling with the -i2 option will cause it to be stored or passed as a 2-byte integer, data type INTEGER*2.
A logical constant is either the logical value true or false. The only logical constants are .TRUE. and .FALSE.; no others are possible. The period delimiters are necessary.
A logical constant takes 4 bytes of storage. If it is an actual argument, it is passed as 4 bytes, unless compiled with the -i2 option, in which case it is passed as 2.
A real constant is an approximation of a real number. It can be positive, negative, or zero. It has a decimal point or an exponent. If no sign is present, the constant is assumed to be nonnegative.
Real constants, REAL*4, use 4 bytes of storage.
A basic real constant consists of an optional plus or minus sign, followed by an integer part, followed by a decimal point, followed by a fractional part.
The integer part and the fractional part are each strings of digits, and you can omit either of these parts, but not both.
Example: Basic real constants:
+82. -32. 90. 98.5
A real exponent consists of the letter E, followed by an optional plus or minus sign, followed by an integer.
E+12 E-3 E6
A real constant has one of these forms:
Basic real constant
Basic real constant followed by a real exponent
Integer constant followed by a real exponent
A real exponent denotes a power of ten. The value of a real constant is the product of that power of ten and the constant that precedes the E.
-32. -32.18 1.6E-9 7E3 1.6E12 $1.0E2.0 Invalid- $ not allowed, error message 82 Not REAL-need decimal point or exponent 29,002.0 Invalid -comma not allowed, error message 1.6E39 Invalid-too large, machine infinity is used 1.6E-39 Invalid -too small, some precision is lost
The restrictions are:
Other than the optional plus or minus sign, a decimal point, the digits 0 through 9, and the letter E, no other characters are allowed.
The magnitude of a normalized single-precision floating-point value must be in the approximate range (1.175494E-38, 3.402823E+38).
A double-precision constant is an approximation of a real number. It can be positive, negative, or zero. If no sign is present, the constant is assumed to be nonnegative. A double-precision constant has a double-precision exponent and an optional decimal point. Double-precision constants, REAL*8, use 8 bytes of storage. The REAL*8 notation is nonstandard. @
A double-precision exponent consists of the letter D, followed by an optional plus or minus sign, followed by an integer.
A double-precision exponent denotes a power of 10. The value of a double-precision constant is the product of that power of 10 and the constant that precedes the D. The form and interpretation are the same as for a real exponent, except that a D is used instead of an E.
Examples of double-precision constants are:
1.6D-9 7D3 $1.0D2.0 Invalid-$ not allowed, error message 82 Not DOUBLE PRECISION-need decimal point or exponent 29,002.0D0 Invalid-comma not allowed, error message 1.8D308 Invalid-too large, machine infinity is used 1.0D-324 Invalid-too small, some precision is lost
The restrictions are:
Other than the optional plus or minus sign, a decimal point, the digits 0 through 9, a blank, and the letter D. No other characters are allowed.
The magnitude of an IEEE normalized double-precision floating-point value must be in the approximate range (2.225074D-308, 1.797693D+308).
(SPARC only) A quadruple-precision constant is a basic real constant or an integer constant, such that it is followed by a quadruple-precision exponent. See "Real Constants". @
A quadruple-precision exponent consists of the letter Q, followed by an optional plus or minus sign, followed by an integer.
A quadruple-precision constant can be positive, negative, or zero. If no sign is present, the constant is assumed to be nonnegative.
Example: Quadruple-precision constants:
1.6Q-9 7Q3 3.3Q-4932 1.1Q+4932 $1.0Q2.0 Invalid-$ not allowed, error message 82 Not quad-need exponent 29,002.0Q0 Invalid-comma not allowed, error message 1.6Q5000 Invalid-too large, machine infinity is used 1.6Q-5000 Invalid-too small, some precision is lost
The form and interpretation are the same as for a real constant, except that a Q is used instead of an E.
The restrictions are:
Other than the optional plus or minus sign, a decimal point, the digits 0 through 9, a blank, and the letter Q. No other characters are allowed.
The magnitude of an IEEE normalized quadruple-precision floating-point value must be in the approximate range (3.362Q-4932, 1.20Q+4932).
It occupies 16 bytes of storage.
Each such datum is aligned on 8-byte boundaries.
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.
The Sun Fortran 77 compiler (release 5.0) recognizes the Fortran 90-style syntax for integer and real constants that allows literal specification of the size of the data item. In Fortran 90 terminology, a constant literal may include an optional trailing underscore followed by a "kind type parameter". @
In the Sun Fortran 77 implementation, the "kind type parameter" is limited to the digits 1, 2, 4, or 8, and its use specifies the data size, in bytes, of the literal constant. For example:
12_8 specifies an 8-byte integer constant, value = 12 12_4 specifies a 4-byte integer constant, value = 12 1.345E-10_8 specifies an 8-byte real constant, value = 1.345E-10 (-1.5_8,.895E-3_8) specifies a complex constant with 8-byte real and imaginary parts
With complex constants, the real and imaginary parts may be specified with different kind type parameters, (1.0_8,2.0_4), but the resulting data item will have the real and imaginary parts with the same size, taking the larger one specified.
This construction is valuable when calling subprograms with constant arguments when a specific data type is required, as in the following example:
call suby(A,1.5_8,0_8,Y) ... subroutine suby(H0, M, N, W) INTEGER *8 M, N, ...