Whats wrong with the following FORTRAN 77 code?

2019-07-22 10:43发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

@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



回答4:

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.