Fortran 中的多字符常量可以在没有任何长度指定的情况下声明吗?

Can a multi-character constant in Fortran be declared without any length specification?

我在编程入门课程中被告知 Fortran95 中的字符常量可以声明为没有任何长度 规范,例如:

character, parameter :: STR = 'Hello World!'

同样的说法可以在 Stephen J. Chapman 的书“Fortran 95/2003 科学家和工程师”(第 34 页),其中说

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!'

我知道可以使用 (len=*)。但实际上是不是 可以完全省略长度选择器吗?

当我使用 GFortran 和 Intel 编译器对其进行测试时,该值为 总是截断到第一个字符。是否有另一个编译器 那支持这个?或者有人可以指点我 Fortran 的一部分 阐明这一点的标准?

Fortran 2008 标准(我指的是官方标准之前的最后一个免费草案)在第 4.4.3.2 节第 4 段(重点是我的)中说:

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.

这表示如果您不通过 *len(LEN=len) 指定长度,则字符长度为 1。对于具有参数属性 (5.3.13) 的字符类型,该标准没有任何例外(我可以找到)。

作者可能拥有一个编译器来实现他作为供应商扩展的声明,但我在标准中找不到它的依据。我用gfortran 5.1.0、ifort 14.0.1和pgf90 11.9测试都符合标准

根据Fortran 2008 Standard,如果没有指定长度,character的长度为1。这在 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.

这与它是字符变量还是命名常量无关。这实际上发生在您的代码中。

相同的行出现在 Fortran 95 Standard and in the Fortran 90 Standard,Cl。 5.1.1.5 在两个文档中。

我猜查普曼是从 FORTRAN 77 Standard, Cl 那里获取信息的。 4.8.1 字符常量,虽然这个子句没有提到命名常量:

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.

除此之外,我无法在任何标准或编译器文档中找到任何参考来支持查普曼的声明。