Fortran

Guide To Learn

Format statements in legacy Fortran code

If you find yourself working with a legacy Fortran program or library for some time, you’ll eventually run into labeled format statements that look like this:

write (*, fmt=1022) x, y, z     ❶
...
1022 format(3(f6.3, 2x))        ❷

❶ Refers to the format statement via a numbered label

❷ Label and format statement

Here, the format string is specified not with a character variable or string literal, but with an integer number that directs the compiler to a labeled line elsewhere in the program. This line then specifies the format string using the format statement. It can be placed anywhere in the local scope–before or after the I/O statement that refers to it. This is a historical, although correct, alternative approach to formatting variables for I/O. Although I don’t recommend that you use the format statement in any new code, I think it’s important that you be aware of its use and meaning, as you may encounter it in existing Fortran code.

Tip Avoid labeled format statements in new code. They break the structure of the code and can make it harder to read and understand. Instead, use literal format strings or character variables in read and write statements directly.

Format statements in legacy Fortran code

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top