Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
504 views
in Technique[技术] by (71.8m points)

fortran - Characters in quotes in a print statement

The documentation for the datetime subroutine in fortran-95, gives an example on the usage of data and time. Here's the code:

program test_time_and_date
    character(8)  :: date
    character(10) :: time
    character(5)  :: zone
    integer,dimension(8) :: values
    ! using keyword arguments
    call date_and_time(date,time,zone,values)
    call date_and_time(DATE=date,ZONE=zone)
    call date_and_time(TIME=time)
    call date_and_time(VALUES=values)
    print '(a,2x,a,2x,a)', date, time, zone
    print '(8i5)', values
end program test_time_and_date

In the above snippet what are the characters inside single quotes, for example '(a,2x,a,2x,a, 8i5)', in the print statements?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Output statements take a format specifier which controls the look of the displayed items. Input statements also use a format to control the interpretation of input.

In the print version of output (compare with write) the format specifier is the part before the (unquoted) comma. In all forms of I/O the format may be specified by one of three ways:

  • using a * (so-called list-directed I/O);
  • using a label for a format statement;
  • using a character expression (variable or constant).

The form in the question is this third.

A character expression for a format specification is made up of a number of parts. The quotes here are simply a delimiter for the string.

First is the mandatory pair of parentheses, leading to the shortest format specification '()' (which prints nothing).

Next, are format items which may be:

  • character literal constants;
  • edit descriptors;
  • control descriptors;
  • a collection of further format items.

Format items may have a repeat count ahead of them.

To answer the question as asked I'll consider just the edit and control descriptors of the format specifications quoted. Wider details of other descriptors (and more detail on the ones here) and formatting may be found elsewhere with the newly gained knowledge on search terms.

There are two edit descriptors here: a and i. There is one control descriptor x.

a specifies output of a string and i an integer. i5 says the output will be width five (blank padded if the integer has fewer than five digits on output); a leads to width the length of the character expression for output.

The control descriptor x inserts a blank. 2x is a blank with repeat count 2: giving two spaces output.

So: the first line prints out three strings with spaces between them (here any trailing blanks from the variables will also appear). The second prints the eight integer elements of the array where each field has a width of five.

Each print statement also gives a newline.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...