Declaration Statements for Character Types

A CHARACTER type specifier can be immediately followed by the length of the character object or function. It takes one of the following forms:

Keyword Forms

CHARACTER [([LEN=]len)]

CHARACTER [([LEN=]len [, [KIND=]n])]

CHARACTER [(KIND=n [, LEN=len])]

Nonkeyword Form

CHARACTER*len[,]

len

Is one of the following:

  • In keyword forms

    The len is a specification expression or an asterisk (*). If no length is specified, the default length is 1.

    If the length evaluates to a negative value, the length of the character entity is zero.

  • In nonkeyword form

    The len is a specification expression or an asterisk enclosed in parentheses, or a scalar integer literal constant (with no kind parameter). The comma is permitted only if no double colon (::) appears in the type declaration statement.

    This form can also (optionally) be specified following the name of the data object or function (v*len). In this case, the length specified overrides any length following the CHARACTER type specifier.

The largest valid value for len in both forms is 2**31-1 on IA-32 architecture; 2**63-1 on Intel® 64 architecture and IA-64 architecture. Negative values are treated as zero.

n

Is a scalar integer initialization expression specifying a valid kind parameter. Currently the only kind available is 1.

Description

An automatic object can appear in a character declaration. The object cannot be a dummy argument, and its length must be declared with a specification expression that is not a constant expression.

The length specified for a character-valued statement function or statement function dummy argument of type character must be an integer constant expression.

When an asterisk length specification *(*) is used for a function name or dummy argument, it assumes the length of the corresponding function reference or actual argument. Similarly, when an asterisk length specification is used for a named constant, the name assumes the length of the actual constant it represents. For example, STRING assumes a 9-byte length in the following statements:

CHARACTER*(*) STRING

PARAMETER (STRING = 'VALUE IS:')

A function name must not be declared with a * length, if the function is an internal or module function, or if it is array-valued, pointer-valued, recursive, or pure.

The form CHARACTER*(*) is an obsolescent feature in Fortran 95.

Examples

In the following example, the character string last_name is given a length of 20:

CHARACTER (LEN=20) last_name

In the following example, stri is given a length of 12, while the other two variables retain a length of 8.

CHARACTER *8 strg, strh, stri*12

In the following example, as a dummy argument strh is given the length of an assigned string when it is assigned, while the other two variables retain a length of 8:

CHARACTER *8 strg, strh(*), stri

The following examples show ways to specify strings of known length:

CHARACTER*32 string

CHARACTER string*32

The following examples show ways to specify strings of unknown length:

CHARACTER string*(*)

CHARACTER*(*) string

The following example declares an array NAMES containing 100 32-character elements, an array SOCSEC containing 100 9-character elements, and a variable NAMETY that is 10 characters long and has an initial value of 'ABCDEFGHIJ'.

CHARACTER*32 NAMES(100),SOCSEC(100)*9,NAMETY*10 /'ABCDEFGHIJ'/

The following example includes a CHARACTER statement declaring two 8-character variables, LAST and FIRST.

INTEGER, PARAMETER :: LENGTH=4

CHARACTER*(4+LENGTH) LAST, FIRST

The following example shows a CHARACTER statement declaring an array LETTER containing 26 one-character elements. It also declares a dummy argument BUBBLE that has a passed length defined by the calling program.

CHARACTER LETTER(26), BUBBLE*(*)

In the following example, NAME2 is an automatic object:

SUBROUTINE AUTO_NAME(NAME1)

CHARACTER(LEN = *) NAME1

CHARACTER(LEN = LEN(NAME1)) NAME2

See Also