10.7: Character Format Specifier
- Page ID
- 54291
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. The w
is the width or how many total places are used. If the width is not specified, the existing length of the string is used. The r
is the number of times the format specifier should be repeated.
A format of '(a6)'
would look like:
c | c | c | c | c | c |
\(\leftarrow \quad\) w \(\quad \rightarrow\) |
For example, given the declarations,
character(len=11) :: msg = "Hello World"
the following write statement can be used to display the string in variable msg
with no leading or trailing spaces. The following write statement,
write (*,'(a11)') msg
will display “Hello World”. The count is not required when using the character format specifier. For example, the statement,
write (*,'(a)') msg
will display the same “Hello World” string. Multiple variables or strings can be displayed. Also, the count can be used to display a portion of the string. For example, to display the string in variable msg
and the string “Goodbye cruel world”.
write (*,'(a9,2x,a)') msg, "Goodbye cruel world"
Which will display “Hello Wor Goodbye cruel world” to the screen. Note that for the first string variable, msg
, only the first 9 characters of the 11 total characters are displayed since the A9 format was used.