Thanks.
Paul Hughett
Regards,
Mike Metcalf
Basically, you are right. However, some additions and - as I hope -
clarifications follow.
Starting with the language standard commonly known as Fortran 90,
there are two possible ways of writing Fortran source code:
(i) "Fixed-form source code" adopts the style required by the lang-
uage standards up to what is commonly called FORTRAN 77,
and
(ii) "Free-form source code" which was newly introduced in For-
tran 90.
Both forms of source code, however, share the concept of limited
line lengths in the source code. Consequently, the question of con-
tinuation lines arises which was not addressed in your posting. (Most
probably, your utility arises from a programming language not impos-
ing such limitations.) :-)
The following applies to fixed-from source code
(i) Lines are _exactly_ 72 characters long. There may be extens-
ions to use columns 73 through 80 for counting purposes but
I am not sure about that. You'd better ask an F77 expert about
that.
If lines contain more than 72 characters, everything beyond
col. 72 is ignored. (Some compilers accept longer lines, but
the standard specifies the 72-col. limit stemming from those
good ol' days of punched cards.) ;-)
If a line contains less than 72 characters, it will be padded
with blanks.
(ii) Cols. 1 trough 5 may contain statement labels (also but false-
ly known as "line numbers") but nothing else.
(iii) Col. 6 may contain a non-blank character. If col. 6 does so,
the respective line is considered to be a continuation line of
the previous one. NB: An exclamation mark (!) appearing in col.
6 makes the line to be a continuation line, _not_ a comment.
The upper limit of continuation lines is 19. Compilers may ac-
cept more than 19 continuation lines.
Comment lines may not be continued.
I am not sure what the standard says about a comment line oc-
curing between a line and its continuation line.
(iv) Cols. 7 through 72 are for Fortran statements. A statement
may begin everywhere as long as it does so between cols. 7
through 72, both inclusive.
(v) Multiple statements on the same line are allowed. Two state-
ments are separated by one semicolon (;) after the first of
them. White spaces before and after the semicolon are optional.
(vi) Comments are indicated
- by a C in col. 1,
- by a c in col. 1. NB: This is a common compiler extension;
the standard did not allow for lowercase letters in FORTRAN 77.
- by an asterisk (*) in col. 1.
- by an exclamation mark (!) in any col. except col. 6 (see item
(iii) above), and both the exclamation mark and anything follow-
ing it is considered to be a comment.
As you have noticed correctly, an exclamation mark inside a string
or Hollerith constant is part of that string or constant, not the
beginning of a comment.
(vii) As you are interested in a line-of-code counter, I will not ad-
dress the significance of white spaces here. The important points
have already been mentioned in items (ii), (iii), and (v) above.
For free-form source code, the following applies:
(i) A line may not exceed 132 characters, starting with col. 1.
(ii) Everything following an exclamation mark (including that exclam-
ation mark) is considered to be a comment. If the exclamation
mark occurs inside a string or Hollerith constant, it does not
indicate the beginning of a comment. This is similar to item
(vi) above.
Comment lines may not be continued.
(iii) A line may contain have up to 39 continuation lines. Continuation
is indicated by an ampersand (&) as the last non-blank character
on the line to be continued.
When a string or Hollerith constant is to be continued on the
next line, the following restrictions apply:
- no comment may follow the ampersand on the line to be continued,
- the next line must not be a comment line,
- the first non-blank character of the continuation line must be
an ampersand.
When a string or Hollerith constant is continued by that double
ampersand, blanks before the first and after the second amper-
sand, respectively, are kept as shown in the following example:
"This is &
& a test."
would give "This is a test." with two white spaces in between
the "is" and the "a". All other white spaces would be ignored.
NB: That statement is based on my personal experience. Maybe,
the standard is different here.
When an element of the Fortran language (name of a function or
subroutine, of a variable or constant, an operator, statement
label or one of the following: =>, (/, /) ) is "broken" by
the end of a line,
- continuation must be indicated by the ampersand (most prob-
ably without any spaces between the end of the lexical tok-
en and the ampersand),
- the first non-blank character on the continuation line must
be an ampersand followed by the "rest" of the token immed-
iately.
(iv) An ampersand may not be the only non-blank character on a line.
As well, an ampersand must not be the only non-blank character
on a line when followed by an exclamation mark.
(v) As above, I will not address the signifiance of blanks here.
Hopefully, that was all, and it was not too confusing.
Happy programming,
ulf
P.S. As Mike Metcalf pointed out correctly, an empty line is considered
to be a comment (some older FORTRAN compilers may not allow empty
lines but that's another topic).
P.P.S. An comment appearing after an END statement does not belong to
the program unit ended by that END statement.
Dick Hendrickson
> I am writing a multilingual lines-of-code counter and want to extend
> it to cover Fortran. As I understand it, the common conventions are a
> that a C or * in column one makes the whole line a comment,
This is only in f77 and fixed-form f90 code.
> and a !
> anywhere in the line (except strings and Hollerith constants) makes
> the rest of the line a comment.
This is only f90 (and later), although many f77 compilers supported this
as an extension.
>Have I missed anything important?
I'm not sure this is "important," but some f77 compilers accept a "D" in
column one as a comment, and have a compiler option that allows these
lines to be treated as regular code. This is for debugging purposes.
$.02 -Ron Shepard
C2345678
0PROGRAM ZERO
0WRITE(6,11)
0CALL EXIT
11 0FORMAT(' HELLO WORLD')
0END
--Jerry Leslie
> I am writing a multilingual lines-of-code counter and want to extend
> it to cover Fortran. As I understand it, the common conventions are a
> that a C or * in column one makes the whole line a comment,
> and a !
> anywhere in the line (except strings and Hollerith constants) makes
> the rest of the line a comment.
> Have I missed anything important?
As well as the points made by others, in FORTRAN 77 and earlier,
anything typed in column 73 and beyond is a comment.
AND: In fixed form, the "!" character in column 6 denotes continuation.
So, assuming your Fortran is F90 (or F77 with the F90 comment
conventions as an extension), a portable way to write comments is
to use "!" exclusively, but avoid putting it in column 6.
--
J. Giles