Difference between 'write' and 'print'
Author Message
Difference between 'write' and 'print'

Hi all. I'm new to fortran. I've bought a book on Fortran 77 because
the algorithm I'm dealing uses that. However the book I referred to
does not seem to have the solution I'm looking for. It's about the
'write' statement.
I know how to print an output using the 'print' and 'format' statements
but this is what is found in the algorithm:
write(*,'(20i6)')
(i,i=1,nclass)
print *
Assuming i have  nclass=5
What does the statement above do? Does the 'i' mean 'integer'
variable???
I would be delighted if a kind soul out there can lend a helping hand
explaining these to me
Thank you!

Sat, 27 Dec 2008 21:45:44 GMT
Difference between 'write' and 'print'

Quote:

> Hi all. I'm new to fortran. I've bought a book on Fortran 77 because
> the algorithm I'm dealing uses that. However the book I referred to
> does not seem to have the solution I'm looking for. It's about the
> 'write' statement.
> I know how to print an output using the 'print' and 'format' statements
> but this is what is found in the algorithm:
>    write(*,'(20i6)') (i,i=1,nclass)
>    print *
> Assuming i have  nclass=5
> What does the statement above do? Does the 'i' mean 'integer'
> variable???

...

Unless you have a _very_ poor book on F77 I'm sure the answer is in
it--the problem is that you're looking in the wrong place, perhaps.
The construct you see is an "implied do".

In a data transfer statement, an implied-do list acts as though it were
a part of an I/O statement within a DO loop. It takes the following
form:

(list, do-var = expr1, expr2 [,expr3])

And, yes "i" here will be an integer "do variable" (although in F77 do
variables could be non-integer, it is truly perverse to do so and it is
almost certainly not the case here).

If the code is old, it probably relies on Fortran implicit typing rules
in which any variable not explicitly typed differently is typed based
on the initial character.  The letters i thru n imply default integers,
anything else default real.

HTH...

Sat, 27 Dec 2008 22:15:36 GMT
Difference between 'write' and 'print'

Quote:

> Hi all. I'm new to fortran. I've bought a book on Fortran 77 because
> the algorithm I'm dealing uses that. However the book I referred to
> does not seem to have the solution I'm looking for. It's about the
> 'write' statement.
> I know how to print an output using the 'print' and 'format' statements
> but this is what is found in the algorithm:
>         write(*,'(20i6)') (i,i=1,nclass)
>         print *
> Assuming i have  nclass=5
> What does the statement above do? Does the 'i' mean 'integer'
> variable???

Personally, I don't like mixing WRITE and PRINT, though both are valid.

I believe that PRINT * is the same as WRITE(*,*), and not that much
more to type.

If someone wants to go through a program and make changes to all WRITE
statements, such as changing the unit number, they will miss PRINT
statements.

Versions of Fortran older than the Fortran 66 standard had PRINT
statements.  PRINT was not included in the 66 standard, but some
(many?) compilers had it as an extension.  It was then added back
in later standards.

Until Fortran 77 Formatted (textual) output required a FORMAT statement
(or variable format).   In Fortran 77 list directed output (* where the
FORMAT statement number goes) was added.  Also, the ability to use a
character string constant containing the format data, instead of a
separate FORMAT statement.

(I believe that was Fortran 77.  Someone will tell me if it came later.)

Fortran 66 and older allowed an array to contain the characters of
a format specification, when variable formatting was needed,
in place of the FORMAT statement number, but not a constant.

-- glen

Sat, 27 Dec 2008 22:46:35 GMT
Difference between 'write' and 'print'

Quote:

> Hi all. I'm new to fortran. I've bought a book on Fortran 77 because
> the algorithm I'm dealing uses that.

You mean the code rather than the algorithm. An algorithm is abstract.
It can be expressed in code, but the code is not the algorithm. Much
like an idea can be expressed in words, but the words are not the idea.
But on to the actual question...

Quote:
> However the book I referred to
> does not seem to have the solution I'm looking for. It's about the
> 'write' statement.
> I know how to print an output using the 'print' and 'format' statements
> but this is what is found in the algorithm:
>     write(*,'(20i6)') (i,i=1,nclass)
>     print *

dpb answered part of the question, mostly focusing on the implied DO. I
had pretty much missed that aspect of the question, my attention being
drawn to your comment that you knew about print and format, but not

1. Write is basically a generalization of print. Or to look at it the
other way, print is a special case of write. Print always goes to the
"standard output" file, while write can go to that file or to any other.
Write can also use lots of other optional features that there just isn't
a place for in the syntax of the print statement. I'll not detail those
here, but there are many of them.

My personal preference is to avoid print. You can always use write, so I
have one less kind of statement to bother with. You cannot always use
print, so if you use print sometimes, you get stuck with using both
statements. I also find the consistency of form handy in that it means I
don't have to change forms if I happen to want to add some option that
isn't available in print. But other people like the simpler form of
print enough to prefer using it. In the end, it is a personal style
choice.

Basically, the relationship is that

print format, io-list

is equivalent to

write (*, format) iolist

The * in the write indicates the "standard output" file that print also
uses; write can also have other things there.

In either form, there are 3 options for the format,

1. A statement number for a format statement. You said you know about
that one.

2. The format as a character expression, most commonly a simple quoted
character literal. That's the '(20I6)' in your write statement. It is
the same as having a format statement with the (20I6), except that you
can put directly in the write statement. You can do much more flexible
(and complicated) things with character expressions, but your simple
example doesn't get into that, so I'll not go into more detail.

3. An asterisk (*) indicating list-directed formatting, also known as
free form. In short (the details are too much to go into here), this is
a quick and easy way to write out data when you are not picky about the
format; it tells the compiler to pick the format details. In the
particular case of your

print *

there is no data, so basically this will just put out a blank line.

Do not confuse this asterisk with the one that means "standard output"
in the write statement. That confusion is easy to do. The write
statement can actually have 2 asterisks, as in

write (*,*)

which does the sam ething as your print * statement. The first * in the
write statement is the one that means "standard output". The second * in
the write statement is format and corresponds to the first (and only) *
in the print statement.

--
Richard Maine                     | Good judgment comes from experience;
email: my first.last at org.domain| experience comes from bad judgment.
org: nasa, domain: gov            |       -- Mark Twain

Sat, 27 Dec 2008 23:15:48 GMT
Difference between 'write' and 'print'

Quote:

...
> > I know how to print an output using the 'print' and 'format' statements
> > but this is what is found in the algorithm:
> >     write(*,'(20i6)') (i,i=1,nclass)
> >     print *

> dpb answered part of the question, mostly focusing on the implied DO. I
> had pretty much missed that aspect of the question, my attention being
> drawn to your comment that you knew about print and format, but not

...

I didn't really even notice the subject line so I thought the confusion
was about what the write statement would actually output.  Between the
two, plus Glen, OP undoubtedly got far more than bargained for... :)

Sun, 28 Dec 2008 03:10:17 GMT
Difference between 'write' and 'print'

(snip)

Quote:
> 1. Write is basically a generalization of print. Or to look at it the
> other way, print is a special case of write. Print always goes to the
> "standard output" file, while write can go to that file or to any other.
> Write can also use lots of other optional features that there just isn't
> a place for in the syntax of the print statement. I'll not detail those
> here, but there are many of them.

It seems that PRINT came first, when there was only one "standard
output" file.

704 Fortran has READ, PRINT, and PUNCH without unit numbers, and
READ INPUT TAPE and WRITE OUTPUT TAPE with a tape unit number and
FORMAT statement number.

(Also, WRITE TAPE and READ TAPE for unformatted tape I/O,
and WRITE DRUM and READ DRUM for unformatted drum I/O.)

The generalization to unit numbers for all devices resulted in
the need to know the unit number for the standard I/O devices.
(The first Fortran system I used had 4 for READ and 5 for WRITE,
to match the number of letters in the word.)  The use of * for
the standard units is nice, though it can't be assigned to a variable.

Quote:
> My personal preference is to avoid print. You can always use write, so I
> have one less kind of statement to bother with. You cannot always use
> print, so if you use print sometimes, you get stuck with using both
> statements.

I agree, mixing the two seems confusing to me.

-- glen

Sun, 28 Dec 2008 05:21:59 GMT
Difference between 'write' and 'print'

Quote:

>> My personal preference is to avoid print. You can always use write, so I
>> have one less kind of statement to bother with. You cannot always use
>> print, so if you use print sometimes, you get stuck with using both
>> statements.

>I agree, mixing the two seems confusing to me.

I use write for permanent output statements, print, for
debugging. The distinction can be helpful later on.

--
Mike Prager, NOAA, Beaufort, NC