Skip to main content
Engineering LibreTexts

10: Formatted Input/Output

  • Page ID
    54210
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Fortran uses a FORMAT statement to allow control of how data is displayed or read. This is useful when very specific input or output is required. For example, displaying money figures typically require exactly two decimal places. There are format specifiers for each data type; integer, real, character, logical, and complex.

    • 10.1: Format
      The format specifiers, separated by commas, are contained in a pair of parenthesis as a string literal. There are multiple possible ways to define a format. However, we will focus on the easiest, most direct method. The format specifier will replace the second “*” in the read or write statements.
    • 10.2: Format Specifiers
      The format specifiers tell the system exactly how the input or output should be handled. Each value being read or written requires some amount of space. For example, an integer of four digits requires at least four spaces or positions to print. Therefore, the number of positions to be used is a key part of the specifier.
    • 10.3: Integer Format Specifier
      The integer format specifier rIw or rIw.m is used tell the system exactly how many positions should be used to either read or write an integer variable.
    • 10.4: Real Format Specifier
      The real format specifier rFw.d is used tell the system exactly how many positions should be used to either read or write a real variable.
    • 10.5: Horizontal Positioning Specifiers
      The horizontal positioning specifiers, nX and Tc, are used to provide specific horizontal spacing or placement as needed.
    • 10.6: Logical Format Specifier
      The logical format specifier rLw is used tell the system exactly how many positions should be used to either read or write an logical variable.
    • 10.7: Character Format Specifier
      The character format specifier rAw is used tell the system exactly how many positions should be used to either read or write a character variable.
    • 10.8: Advance Clause
      The advance clause instructs the computer whether or not to advance the cursor to the next line.
    • 10.9: Example
      This example will read a date from the user (month, day, and year, on the same line), determine the day of week (for that month/day/year). Then, the program will display the original input date (numeric form) and the formatted date.
    • 10.10: Exercises


    This page titled 10: Formatted Input/Output is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?