Reputation: 43
I've been told in an introductory course on programming that a character constant in Fortran95 can be declared without any length specification, e.g.:
character, parameter :: STR = 'Hello World!'
The same statement can be found in Stephen J. Chapman's book “Fortran 95/2003 for Scientists and Engineers” (p. 34), which says
If the named constant is of type CHARACTER, then it is not necessary to declare the length of the character string. Since the named constant is being defined on the same line as its type declaration, the Fortran compiler can directly count the number of characters in the string. For example, the following statements declare a named constant error_message to be the 14-character string ‘Unknown error!’.
CHARACTER, PARAMETER :: ERROR_MESSAGE = 'Unknown error!'
I know that it is possible to use (len=*)
. But is it actually
possible to omit the length selector entirely?
When I tested it with the GFortran and Intel compilers, the value was always truncated to the first character. Is there another compiler that supports this? Or can someone point me to a part of the Fortran standard which clarifies this?
Upvotes: 4
Views: 632
Reputation: 18118
By the Fortran 2008 Standard, the character
has a length of one, if no length is specified. This is given in Cl. 4.4.3.2 "Character type specifier":
4 [...] If the length is not specified in a char-selector or a * char-length, the length is 1.
This is independent of whether it is a character variable or named constant. This actually happens in your code.
The same line is present in the Fortran 95 Standard and in the Fortran 90 Standard, Cl. 5.1.1.5 in both documents.
I guess Chapman takes his information from the FORTRAN 77 Standard, Cl. 4.8.1 Character Constant, although this clause does not refer to named constants:
The length of a character constant is the number of characters between the delimiting apostrophes, except that each pair of consecutive apostrophes counts as a single character. The delimiting apostrophes are not counted. The length of a character constant must be greater than zero.
Apart from that, I could not find any reference in any Standard or compiler documentation to support Chapman's statement.
Upvotes: 3
Reputation: 6915
The Fortran 2008 standard (I'm referencing the last freely available draft ahead of the official standard) says in section 4.4.3.2, paragraph 4 (emphasis is mine):
The char-selector in a CHARACTER intrinsic-type-spec and the * char-length in an entity-decl or in a component- decl of a type definition specify character length. The * char-length in an entity-decl or a component-decl specifies an individual length and overrides the length specified in the char-selector , if any. If a * char-length is not specified in an entity-decl or a component-decl , the length-selector or type-param-value specified in the char-selector is the character length. If the length is not specified in a char-selector or a * char-length, the length is 1.
This says that if you do not specify a length via *len
or (LEN=len)
then the character length is 1
. The standard does not make any exception (that I can find) for character types with the parameter attribute (5.3.13).
It is possible the author possessed a compiler implementing his claim as a vendor extension but I cannot find basis for it in the standard. I tested with gfortran 5.1.0, ifort 14.0.1 and pgf90 11.9 and all conform to the standard.
Upvotes: 1