Fortran Variable Declarations
Declaring the type of a Fortran variable is done with
type statements. It has the following form:
type-specifier :: list
where the type-specifier is one of the following and list is
a list of variable names separated with commas:
- INTEGER :
the variables in list can hold integers
- REAL:
the variables in list can hold real numbers
- COMPLEX:
the variables in list can hold complex numbers
- LOGICAL:
the variables in list can hold logical values
(i.e., true or false)
- CHARACTER:
the variables in list can hold character strings
Types INTEGER and REAL
are easy. The following are examples:
- Variables ZIP, Mean and Total are of type
INTEGER:
INTEGER :: ZIP, Mean, Total
- Variables Average, error, sum and ZAP
are of type REAL:
REAL :: Average, error, sum, ZAP
Type CHARACTER is more involved.
Since a string has a length
attribute, a length value must be attached to character variable
declarations. There are two ways to do this:
- Use CHARACTER(LEN=i) to declare character
variables of length i. For examples,
- Name and Street are character variables
that can hold a string of no more than 15 characters:
CHARACTER(LEN=15) :: Name, Street
- FirstName, LastName and OtherName
are character variables that can hold a string of no
more than 20 characters:
CHARACTER(LEN=20) :: FirstName, LastName, OtehrName
- Use CHARACTER(i) to declare character
variables of length i. That is, there is no
LEN= in the parenthesis. For examples,
- Name and Street are character variables
that can hold a string of no more than 15 characters:
CHARACTER(15) :: Name, Street
- FirstName, LastName and OtherName
are character variables that can hold a string of no
more than 20 characters:
CHARACTER(20) :: FirstName, LastName, OtehrName
- If a variable can only hold a single character, the length
part can be removed. The following three declarations are all
equivalent:
CHARACTER(LEN=1) :: letter, digit
CHARACTER(1) :: letter, digit
CHARACTER :: letter, digit
Here, variables letter and digit can only hold no
more than one character.
If you want to declare character variables of different length
with a single statement, you can attach a length specification,
*i, to the right of a variable. In this case,
the corresponding variable will have the indicated length and all
other variables are not affected.
CHARACTER(LEN=10) :: City, Nation*20, BOX, bug*1
Here, variables City and BOX can hold a string of
no more than 10 characters, Nation can hold a string of
no more than 20 characters, and bug can hold only one
character.
There is one more way of specifying the length of a character
variable. If the length value is replaced with a asterisk
*, it means the lengths of the declared variables are
determined elsewhere. In general, this type of declarations is used
in subprogram arguments or in
PARAMETER
and is refereed to as
assumed length specifier.
CHARACTER(LEN=*) :: Title, Position
Here, the actual lengths of variables Title and
Position are unknown and will be determined elsewhere.