The assignment statement assigns a value to a variable, substring, array element, record, or record field.
Parameter |
Description |
v |
Variable, substring, array element, record, or record field |
e |
Expression giving the value to be assigned |
The value can be a constant or the result of an expression. The kinds of assignment statements: are arithmetic, logical, character, and record assignments.
v is of numeric type and is the name of a variable, array element, or record field.
e is an arithmetic expression, a character constant, or a logical expression. Assigning logicals to numerics is nonstandard, and may not be portable; the resultant data type is, of course, the data type of v. @
Execution of an arithmetic assignment statement causes the evaluation of the expression e, and conversion to the type of v (if types differ), and assignment of v with the resulting value typed according to the following table.
Type of v |
Type of e |
---|---|
INTEGER*2, INTEGER*4, or INTEGER*8 REAL REAL*8 REAL*16 (SPARC only) DOUBLE PRECISION COMPLEX*8 COMPLEX*16 COMPLEX*32 (SPARC only) |
INT(e) REAL(e) REAL*8 QREAL(e) (SPARC only) DBLE(e) CMPLX(e) DCMPLX(e) QCMPLX(e) (SPARC only) |
Compiling with any of the options -i2, -dbl, -r8, or -xtypemap can alter the default data size of variables and expressions. This is discussed in Chapter 2. See also the Fortran User's Guide for a description of these options.
Example: An assignment statement:
REAL A, B DOUBLE PRECISION V V = A * B
The above code is compiled exactly as if it were the following:
REAL A, B DOUBLE PRECISION V V = DBLE( A * B )
v is the name of a variable, array element, or record field of type logical.
e is a logical expression, or an integer between -128 and 127, or a single character constant.
Execution of a logical assignment statement causes evaluation of the logical expression e and assignment of the resulting value to v. If e is a logical expression (rather than an integer between -128 and 127, or a single character constant), then e must have a value of either true or false.
Logical expressions of any size can be assigned to logical variables of any size. The section on the LOGICAL statement provides more details on the size of logical variables.
The constant can be a Hollerith constant or a string of characters delimited by apostrophes (') or quotes ("). The character string cannot include the control characters Control-A, Control-B, or Control-C; that is, you cannot hold down the Control key and press the A, B, or C keys. If you need those control characters, use the char() function.
If you use quotes to delimit a character constant, then you cannot compile with the -xl option, because, in that case, a quote introduces an octal constant. The characters are transferred to the variables without any conversion of data, and may not be portable.
Character expressions which include the // operator can be assigned only to items of type CHARACTER. Here, the v is the name of a variable, substring, array element, or record field of type CHARACTER; e is a character expression.
Execution of a character assignment statement causes evaluation of the character expression and assignment of the resulting value to v. If the length of e is more than that of v, characters on the right are truncated. If the length of e is less than that of v, blank characters are padded on the right.
v and e are each a record or record field. @
The e and v must have the same structure. They have the same structure if any of the following occur:
Both e and v are fields with the same elementary data type.
Both e and v are records with the same number of fields such that corresponding fields are the same elementary data type.
Both e and v are records with the same number of fields such that corresponding fields are substructures with the same structure as defined in 2, above.
The sections on the RECORD and STRUCTURE statements have more details on the structure of records.
Example 1: Arithmetic assignment:
INTEGER I2*2, J2*2, I4*4 REAL R1, QP*16 ! (REAL*16 is SPARC only) DOUBLE PRECISION DP COMPLEX C8, C16*16, QC*32 ! (COMPLEX*32 is SPARC only) J2 = 29002 I2 = J2 I4 = (I2 * 2) + 1 DP = 6.4D9 QP = 6.4Q9 R1 = DP C8 = R1 C8 = ( 3.0, 5.0 ) I2 = C8 C16 = C8 C32 = C8
Example 2: Logical assignment:
LOGICAL B1*1, B2*1 LOGICAL L3, L4 L4 = .TRUE. B1 = L4 B2 = B1
Example 3: Hollerith assignment:
CHARACTER S*4 INTEGER I2*2, I4*4 REAL R S = 4Hwxyz I2 = 2Hyz I4 = 4Hwxyz R = 4Hwxyz
Example 4: Character assignment:
CHARACTER BELL*1, C2*2, C3*3, C5*5, C6*6 REAL Z C2 = 'z' C3 = 'uvwxyz' C5 = 'vwxyz' C5(1:2) = 'AB' C6 = C5 // C2 BELL = CHAR(7) Control Character (^G)
C2 C3 C5 C6 |
receives 'zD' a trailing blank receives 'uvw' receives 'ABxyz' receives 'ABxyzz' an extra z left over from C5 |
BELL |
receives 07 hex Control-G, a bell |
:
Example 5: Record assignment and record field assignment:
STRUCTURE /PRODUCT/ INTEGER*4 ID CHARACTER*16 NAME CHARACTER*8 MODEL REAL*4 COST REAL*4 PRICE END STRUCTURE RECORD /PRODUCT/ CURRENT, PRIOR, NEXT, LINE(10) ... CURRENT = NEXT Record to record LINE(1) = CURRENT Record to array element WRITE ( 9 ) CURRENT Write whole record NEXT.ID = 82 Assign a value to a field