What is the meaning of the following character declaration
character :: c*4
Is it in anyway special for characters or does it apply to all data types?
Is the 4 the same as the length parameter as in
character(len=4) :: c
What is the meaning of the following character declaration
character :: c*4
Is it in anyway special for characters or does it apply to all data types?
Is the 4 the same as the length parameter as in
character(len=4) :: c
There are several ways to declare the length of a character entity in a type declaration statement, but explanations of these are spread across several other questions and answers, so let's gather them here. The forms of the question have the same effect.
A character declaration statement may specify the length in the type specifier using the len=
form:
character(len=4) :: ... ! Literal constant length
character(len=n) :: ... ! Named constant/variable length
character(len=*) :: ... ! Assumed/implied length
character(len=:) :: ... ! Deferred length
or with the *
form:
character*4 :: ... ! Literal constant length
character*(4) :: ... ! Another literal constant length
character*(n) :: ... ! Named constant/variable length
character*(*) :: ... ! Assumed/implied length
character*(:) :: ... ! Deferred length
For the case of a literal constant, the parentheses are optional and not necessary, but are necessary for other cases.
Alternatively, the *
form may be used in the entity declaration itself:
character :: a*4, b*(n), c*(*), d*(:)
character(len=2) :: x*4 ! The *4 overrides the len=2
In all cases, 1
is the default length if no value is specified.
If you want to specify length and array shape in this way:
character :: a(5,5)*4
character(len=2), dimension(2) :: b(5,5)*4 ! Shape and length overridden.
The form of specification using *
is unique to character lengths (for functions and variables). Even non-character objects with length type parameters cannot use this syntax. However, as Vladimir F notes, there is the similar non-standard form like integer*4
.
Finally, the name*(len)
form is specific to type declaration statements. It can't be used, for example, in allocation:
character(:), allocatable :: c
allocate(character :: c*4) ! Not allowed as length specification, use instead
allocate(character(len=4) :: c) ! or
allocate(character*4 :: c) ! etc
Yes, they are both strings of the same length. See also Difference between “character*10 :: a” and “character :: a(10)”.
The syntax is completely special for characters and cannot be used for other datatypes. Re-using the syntax for other datatypes like integer*4
might be motivated by the old Hollerith editing, where characters were stored in integer numbers, but is completely non-standard.