Skip to main content
Engineering LibreTexts

6.1: Output – Write

  • Page ID
    54263
  • \( \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}}\)

    As noted from the first program, simple output can be performed by using the write statement. For example:

    write (*,*) "Hello World"
    

    Which will send the message, referred to as a string, Hello World to the screen. The first “*” means the default output device, which is the screen or monitor. The second “*” refers to the 'free format'. Thus, the “(*,*)” means to send it to the screen in 'free format'.

    The free format allows the Fortran compiler to determine the appropriate format for the information being displayed. This is easy, especially when first getting started, but does not allow the program much control over how the output will be formatted or displayed on the screen.

    Additionally, the value held by declared variables can be displayed. For example, to declare the variables num1, num2, and num3.

    integer :: num1=20, num2=50, num3=10
    

    the write statement to display num1 would be,

    write (*,*) num1
    

    The free format allows the Fortran compiler to determine the appropriate output format for the information being displayed.

    A write statement with no strings or variables,

    write (*,*)
    

    will display a blank line.

    Multiple variables and strings can be displayed with one write statement. For example, using the previous declarations;

    write (*,*) "Number 1 = ", num1, "Number 2 = ", num2
    

    The information inside the quotes is displayed as is, including capitalization and any spelling errors. When the quotes are not used, it is interpreted as a variable. If the variable is not declared, a compiler error will be generated. The value assigned to each variable will be displayed. A value must have been assigned to the variable prior to attempting to display.

    Output — Print

    In addition to the write statement, a print statement can be used. The print statement will send output only to the screen. Thus, it is a more restrictive form of the write statement.

    As with the write statement, multiple variables and strings can be displayed with one print statement. For example, using the previous declarations,

    print *,"Number 1 = ", num1, "Number 2 = ", num2
    

    The information inside the quotes is displayed as is, including capitalization and any spelling errors. When the quotes are not used, it is interpreted as a variable. If the variable is not declared, an error will be generated. If the variable is defined, the value assigned to that variable will be displayed.

    In general, all examples will use the write statement.


    This page titled 6.1: Output – Write 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?