# Declarations for Character Types

A CHARACTER type specifier can be immediately followed by an optional KIND type parameter and 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])]

Obsolete Form

CHARACTER* len[,]

len | Is a length type parameter. It can have one of the following values: A scalar integer expression * : The following rules also apply: In keyword forms The len is a specification expression, an asterisk (*), or a colon (:). 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 the obsolete 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. Negative values are treated as zero.
n | Is a scalar integer constant 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 unless it is of type CHARACTER and is the name of the result of an external function or the name of a dummy function.

A function name declared with a * length must not be an array, a pointer, recursive, elemental, or pure.

If the CHARACTER type declaration statement specifies a colon (:) length, the length type parameter is a deferred type parameter. An entity or component with a deferred type parameter must specify the ALLOCATABLE or POINTER attribute. A deferred type parameter is a length type parameter whose value can change during execution of the program.

The obsolete form is an obsolescent feature in the Fortran standard.

## 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, a dummy argument strh will assume the length of the associated actual argument, 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 string1
      CHARACTER string2*32

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

      CHARACTER string3*(*)
      CHARACTER*(*) string4

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

The following example shows the handling of a deferred-length CHARACTER variables:

    CHARACTER(:), ALLOCATABLE :: namea
    namea = 'XYZ'                   ! Allocates namea as length 3 with value 'XYZ'
    DEALLOCATE (namea)
    ALLOCATE (CHARACTER(10)::namea) ! Allocates namea as length 10, no value
    namea = 'ABCDEF'                ! Reallocates namea to length 6 with value 'ABCDEF'

Parent topic: Type Declarations

## See Also

Obsolescent Language Features in the Fortran Standard
Data Types of Scalar Variables
Assumed-Length Character Arguments for details on asterisk length specifications
Type Declarations for details on the general form and rules for type declaration statements