In this section, you’ll learn how to explicitly format numbers and text for output to screen or files on disk. You’ve probably noticed that in most cases when we used print or write statements to display values on the screen, they produced output with somewhat awkward formatting and wide empty spaces between values. For example, in the previous section, we used list-directed I/O to read a few text and numerical variables from standard input and write them back to the screen:
Hello 42 3.14159 ❶
User typed: Hello 42 3.14159012 ❷
❶ Values for each variable typed in by the user
Notice the odd empty space between “Hello” and 42. Worse, when writing the number 3.14159 back to the screen, the program output “3.14159012,” which is a similar but nevertheless different number. What the heck is going on here?
These inconsistencies are due to the default formatting that’s compiler- and system-dependent. Anytime that we use read *, print *, or write(*,*) statements, we instruct the compiler to use any format it sees fit. Default formatting is fine for quick and dirty printing of values from your program. However, if you want your app to produce human-readable output, or write results in a portable format, you’ll need to explicitly format your output. In this section, you’ll learn how to compose the format strings (or, in Fortran terminology, edit descriptors) to control how the values of variables should be converted to text data for output to screen or files. Formatting your output will prove important in any production app or system where the output of your program is either directly consumed by your users or used as input to some other program.