Next: READ
Up: Input and Output
Previous: Input/Output Options

WRITE

Take a look at ``print.f'' in your ``examples'' directory (or view it directly). This is a very straightforward program that prints out the number 20 in four different ways. Notice that the first way involves the familiar ``PRINT'' statement, and the final three ways involve the ``WRITE'' statement, which you haven't seen before.

Compile and run the program. Let's consider the four different statements in turn.

  1. The first reads

          PRINT *, 20
    

    This is the simple output statement with which you are familiar. As you have learned Fortran, you may have been wondering what the ``*'' means in the PRINT statement. It is actually a default format specification. What this means is that when you use a ``PRINT'' statement, you are allowing Fortran to decide exactly how to format the appearance of the output. Shortly, we'll see how you can exercise more control.

  2. The second reads

          WRITE (UNIT=*, FMT=*) 20
    

    With the ``WRITE'' statement, you provide two pieces of information that control where output is to be written and how it is to be formatted. The UNIT=* part controls where, and the FMT=* part controls how. As with the ``PRINT'' statement, ``*'' leaves the choice up to Fortran. By specifying UNIT=*, we are telling Fortran to do the output to the display; and by specifying FMT=*, we are allowing Fortran to decide how to format the output.

    This ``WRITE'' statement is functionally identical to the simple PRINT statement from part (1). ``WRITE'' statements are interesting because they allow us to specify values for UNIT and FMT that are different from the defaults used by PRINT, as we will see shortly.

  3. The third reads

          WRITE (UNIT=6, FMT=*) 20
    

    Here, we are specifying that output is to go to unit 6 using the default formatting conventions. Unit 6 is the display, so this statement will print to the display just like the first two. But by specifying units other than 6, we can arrange to send output to files instead. You will learn how to do this in an upcoming lesson.

  4. Finally, the fourth statement reads

          WRITE (UNIT=6, FMT=100) 20
    

    Now we are telling Fortran to use format 100. How does Fortran know what format 100 is?

    Click here for the answer

Change the program so that instead of printing 20, it prints 2000. What happens when you run it?

Click here for the answer

Which of these many forms should you use? The answer depends upon what you are trying to do. If you just want to print some stuff out to the display and don't care about the format, you should use

      PRINT *, ....

If you care about the format, or if you want to print to somewhere other than the display, you should use (for example)

      WRITE (UNIT=8, FMT=200)

This would print to unit 8, using format 200. Of course, this assumes that you have told Fortran what unit 8 is and what format 200 means.

In this section we have just tried to give you an overview of what is possible with output. What are the two primary things that you still need to know?

Click here for the answer

You will learn these two things in upcoming lessons.


Next: READ
Up: Input and Output
Previous: Input/Output Options

Hamlet Project
hamlet@cs.utah.edu