CHARACTER type

Standard: F77 F90 F95 F2003 F2008 F2018 Example program


<Syntax>

CHARACTER [ char-spec ]

char-spec ::= * int-literal-constant
| * ( char-len )
| ( char-len [ , [ KIND= ] expr )
| ( LEN=char-len [ , KIND=expr ] )
| ( KIND=expr [ , LEN=char-len ] )

char-len ::= * | expr | :

<Semantics>

CHARACTER declares entities (variables, functions, etc.) to be character strings. The length of the character string is given by the int-literal-constant or char-len. If no length is specified, the length of the string is 1 character.

A char-len of “*” is allowed for a PARAMETER or a dummy argument; the length of the PARAMETER is taken from the value given for it, and the length of the dummy argument is taken from the actual argument at runtime.

A char-len of “:” is allowed for an entity with the ALLOCATABLE or POINTER attribute; this is called “deferred character length” and the actual length is determined at runtime by allocation or association.

Furthermore, assignment to a whole allocatable variable that has deferred character length will reallocate the variable to have the same character length as the right-hand-side of the assignment if either the variable was unallocated or if the length was different. This provides a true variable-length character facility. The “automatic reallocation” can be suppressed by using substring (or for an array, array section) notation if it is not desired.

There are four kinds of CHARACTER as follows:

KIND SELECTED_CHAR_KIND F90_KIND constant Description
1 'ASCII' ASCII Single byte character
2 'JIS_0213' JIS 16-bit Japanese character
3 'UCS_2' UCS2 16-bit Unicode character
4 'ISO_10646' UCS4 32-bit Unicode character

Note that the KIND column is for sequential or byte kinds (-kind=sequential and -kind=byte options); the values are completely different for the -kind=unique option.

The default if no kind is specified is single byte character (ASCII).

Note that for CHARACTER literal constants, the kind specification precedes the value (unlike other types where it follows the value). For example, using the named constant in the F90_KIND module,
      JIS_"月"
has the same value as
      CHAR(35982,JIS)

The length in the CHARACTER clause may be overridden by putting “*int-literal-constant” or “*(char-len)” after the name of the entity in the declaration; for example,
      Character(17) :: a, b*3, c
declares A and C to have length 17 and B to have length 3.

<Related>

ACHAR intrinsic function, ALLOCATABLE attribute, CHAR intrinsic function, COMPLEX type, DOUBLE PRECISION type, IACHAR intrinsic function, ICHAR intrinsic function, IMPLICIT statement, INTEGER type, KIND intrinsic function, LEN intrinsic function, LOGICAL type, POINTER attribute, REAL type, SELECTED_CHAR_KIND intrinsic function, Type Declaration statement