Is initializing a variable in between variable declarations forbidden?

后端 未结 3 1206
日久生厌
日久生厌 2021-01-23 09:42

I have something like

integer a
integer b 
b = 0
integer c

a = 0
c = 0

which does not work with the error

\"A specific

3条回答
  •  不知归路
    2021-01-23 10:22

    Yes, this is forbidden in Fortran. This is defined in the Fortran 2008 Standard, Cl. 2.3.2 "Statement order":

    1 The syntax rules of clause 2.1 specify the statement order within program units and subprograms. These rules are illustrated in Table 2.1 [...]. Table 2.1 shows the ordering rules for statements and applies to all program units, subprograms, and interface bodies. Vertical lines delineate varieties of statements that may be interspersed and horizontal lines delineate varieties of statements that shall not be interspersed. [...] Between USE and CONTAINS statements in a subprogram, nonexecutable statements generally precede executable statements [...]

    (Emphasis mine)


    [Slightly off-topic, but related] Please note that while

    integer :: a
    integer :: b = 0
    integer :: c
    

    is allowed, this has the side effect that b gets the save attribute. That is typically not what you want...

提交回复
热议问题