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

标签 fortran fortran95

我在编程的介绍性类(class)中被告知
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 Standard , character如果未指定长度,则长度为 1。这在 Cl 中给出。 4.4.3.2“字符类型说明符”:

4 [...] If the length is not specified in a char-selector or a * char-length, the length is 1.



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

相同的行存在于 Fortran 95 Standard 中并在 Fortran 90 Standard ,氯。 5.1.1.5 在两个文件中。

我猜查普曼从 FORTRAN 77 Standard 获取他的信息,氯。 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.



除此之外,我在任何标准或编译器文档中都找不到任何引用来支持 Chapman 的声明。

关于fortran - 可以在没有任何长度规范的情况下声明 Fortran 中的多字符常量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30037623/

相关文章:

arrays - 如何选择二维数组 A(i,j) 的最佳配置

networking - 如何在 Fortran 中连接到网络?

inheritance - 了解 Fortran 扩展类型和覆盖

file-io - Fortran 95 : open statement, 状态变量 : unknown vs. 替换

fortran - 在一条语句中对多个变量进行相同的赋值

matrix - 求解受约束的线性方程组

io - 使用 MPI-IO 编写 Fortran 格式的文件

c++ - C++ 中的 std::complex<double> 与具有链接时优化的 gcc 6 下 Fortran 中的 complex*16 之间的类型不匹配

fortran - 将一个基本过程传递给另一个基本过程

fortran - 包含的子例程中变量和函数的作用域规则