Here are examples of using the print statement with formatting.

character(len=12)      :: c = 'abcdefghijkl'
integer                :: i = 10000
real                   :: r = 3.14159e2
logical                :: l = .true.

print '(a)', c         ! abcdefghijkl
print '(a4)', c        ! abcd         ! Truncated
print '(i8)', i        !    10000     ! Right-justified
print '(i4)', i        ! ****         ! If the number cannot be
                                      ! printed in the available
                                      ! field, asterisks appear

print '(f12.5)', r     !    314.15900 ! Zero-padded
print '(f12.2)', r     !       314.16 ! Rounded
print '(f6.4)', r      ! ******       ! Not enough space again

print '(e12.5)', r     !  0.31416e+03 ! Format with exponent, rounded,
                                      ! number starts with zero

print '(es12.5)', r    !  3.14159e+02 ! Format with exponent

print '(l)', l         ! T            ! T or F is printed for
print '(l2)', l        !  T           ! .true. or .false.
Fortran

Formats can be repeated and grouped by using parentheses (). To repeat a format, add a preceding number. Example:

print '(3es12.5)', x, y, z           ! Three numbers are printed
                                     ! on one line with the
                                     ! same format (es12.5)

print '(3(es12.5,1x))', x, y, z      ! Same as above, with a
                                     ! whitespace added

print '(3(es12.5,2x))', x, y, z      ! Same as above, with two
                                     ! whitespaces added
Fortran

The write statement works similarly to the print statement, but it allows one to specify where to print. The general form of the write statement is:

write (unit, format) list
Fortran

The unit is a number; the format is a format string. The format is the same as for the print statement. The unit number is a handle that points to the destination. Standard output (unit number 6) and standard error (unit number 0) are always accessible and do not have to be opened by the program. If the output is to be written to a file, then the file has to be opened with the open statement first (see below).

Examples: Writing to standard output. The standard output channel has the unit number 6. An asterisk (*) can also be used.

character(len=8) :: name = 'Karl'
integer          :: iage = 25
print *,            'My name is ', name   !  My name is Karl
print *,            'My age is ', iage    !  My age is           25
write (6,'(a,a8)')  'My name is ', name   ! My name is Karl
write (6,'(a,i4)')  'My age is ',  iage   ! My age is   25
write (*,'(a,a8)')  'My name is ', name   ! My name is Karl
write (*,'(a,i4)')  'My age is ',  iage   ! My age is   25
write (*,*)         'My age is ',  iage   !  My age is           25
Fortran

Examples: Same as before, but writing to standard error. The error channel has the unit number 0.

write (0,'(a,a8)')  'My name is ', name   ! My name is Karl
write (0,'(a,i4)')  'My age is ',  iage   ! My age is   25
write (0,*)         'My age is ',  iage   !  My age is           25
Fortran

Unit number 5 is reserved for reading from standard input. An asterisk (*) can also be used. Example:

read (5,'(i4)') iage     ! Read iage from standard input
Fortran

The next example demonstrates how to prompt for input to read data:

write (6,'(a)') 'Please enter your age : '
read  (5,'(i4)') iage
Fortran

To avoid the carriage return of the write statement, i.e. to have the prompt right after the string, add the advance='no' specifier:

write (6,'(a)',advance='no') 'Please enter your age : '
read  (5,'(i4)') iage
Fortran
 
© 2025  |   Cornell University    |   Center for Advanced Computing    |   Copyright Statement    |   Access Statement
CVW material development is supported by NSF OAC awards 1854828, 2321040, 2323116 (UT Austin) and 2005506 (Indiana University)