以下 FORTRAN 77 代码有什么问题?

发布于 2024-11-17 06:30:36 字数 349 浏览 5 评论 0原文

我是一个 FORTRAN 77 新手,我不明白为什么第一个代码显示错误,而第二个代码在我期望它们做同样的事情时编译。

第一个代码(不会编译并给出错误,引用 z 处的意外数据声明语句):

program FOO

integer x, y

x = 1
y = 2

integer z 

z = 3

end

此代码在功能上看起来与第一个代码 100% 相似,编译时没有错误

program FOO

integer x, y, z

x = 1
y = 2
z = 3

end

我还尝试在第一个代码中禁用隐式变量声明没有影响。

I am a total FORTRAN 77 newbie, and I don't understand why the first code shows an error while the second one compiles when I expect them to do the same.

First code (which doesn't compile and gives a error citing an unexpected data declaration statement at z):

program FOO

integer x, y

x = 1
y = 2

integer z 

z = 3

end

This code which looks 100% similar in functionality to the first one compiles without errors

program FOO

integer x, y, z

x = 1
y = 2
z = 3

end

I also tried disabling implicit variable declarations in the first code with no effects.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

离线来电— 2024-11-24 06:30:36

Fortran 是一种奇特的“在顶层定义一切”语言。换句话说,这很好:

program FOO
    integer x, y
    integer z 
    x = 1
    y = 2
    z = 3
end

因为所有类型规范都位于任何可执行代码之前。如果您定义一个变量,您应该首先定义它。请参阅此处例如:

此类不可执行的语句必须放置在程序的开头、第一个可执行语句之前。

Fortran is one of those quaint "define everything at the top" languages. In other words, this would be fine:

program FOO
    integer x, y
    integer z 
    x = 1
    y = 2
    z = 3
end

since all type specifications are before any executable code. If you're going to define a variable, you should define it first. See here for example:

Such non-executable statements must be placed at the beginning of a program, before the first executable statement.

栀子花开つ 2024-11-24 06:30:36

我不知道真正的解决方案,但也许 fortran77 不支持变量之间的任何代码。

例如;

integer x, y, z

x = 1
y = 2
z = 3

有效但

integer x, y

x = 1
y = 2

integer z 

z = 3

不起作用。因为在两个整数定义之间(integer x, yinteger z ),存在变量赋值。

I don't know real solution but maybe fortran77 doesn't support any code between variables.

for example;

integer x, y, z

x = 1
y = 2
z = 3

works but

integer x, y

x = 1
y = 2

integer z 

z = 3

doesn't work. Because between two integer definening (integer x, y and integer z ), there are variables assigning.

橘味果▽酱 2024-11-24 06:30:36

@paxdiablo:你想得对!

和错误消息:

“...意外的数据声明语句在...”

所有 DELCARATION 必须在第一个 STATMENT 出现之前进行。 fortran77 真的很“旧”,我不确定 F95 中是否会改变这一点

@paxdiablo: you think right!

and the errormessage:

"... unexpected data declaration statement at ..."

all DELCARATION must be made BEFORE the first STATEMENT occurs. fortran77 is really "old", I´m not shure if this is changed in F95

物价感观 2024-11-24 06:30:36

供您参考:禁用隐式变量声明只会消除 Fortan 假设变量类型的能力。

隐式变量声明做出以下假设: 任何以(大写或小写)开头的变量:I、J、K、L、M 或 N 均为 INTEGER。任何以任何其他字母(大写或小写)开头的变量都是 REAL。这仅适用于没有显式类型声明的变量。

您可以编写:

program FOO
ijk
ifjkask    
end

ijk 和 ifjkask 将为 INTEGER 值。

For your information: Disabling implicit variable declarations simply removes Fortan's ability to make assumptions about what type your variables are.

Implicit variable declaration makes the following assumptions: Any variable beginning with (capital or lowercase): I, J, K, L, M, or N is to be INTEGER. Any variable beginning with any other letter (capital or lowercase) is to be REAL. This applies only to variables which do not have an explicit type declaration.

You could write:

program FOO
ijk
ifjkask    
end

and ijk and ifjkask would be INTEGER values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文