Strings and Internal I/O
Character Strings
Length and Trim
A fixed-length character variable pads short values with blanks. len_trim returns the logical length.
Program
Play the program to store Ada in a length-20 buffer and read its trimmed length.
character_strings.f90
Replay: real traced execution (multi-file project)
program character_strings
implicit none
character(len=20) :: name
integer :: name_length
name = "Ada"
name_length = len_trim(name)
print '(I0)', name_length
end program character_strings
name ← Ada (padded to len=20)
4integer :: name_length5name = "Ada"6name_length = len_trim(name)values this stepAda (padded to len=20)namename_length ← 3
5name = "Ada"6name_length = len_trim(name)7print '(I0)', name_lengthvalues this step3name_lengthAda (padded)nameprint '(I0)', name_length
6 name_length = len_trim(name)7 print '(I0)', name_length8end program character_stringsoutput3values this step3name_length
Follow the Values
namestoresAdain acharacter(len=20)variable.- The stored text is padded with blanks up to length 20.
len_trim(name)ignores the trailing blanks.name_lengthbecomes3, so the program prints3. | value | what it means | | --- | --- | |Ada| visible letters inname| |20| fixed storage length | |3| trimmed length printed |
fixed-length
`character(len=20)` always stores exactly 20 characters.
padding
Shorter values are padded with trailing blanks.
len_trim
`len_trim` returns the position of the last non-blank.
Exercise: character_strings.f90
Reproduce the output 3, then trace why the length-20 storage still prints a trimmed length of 3.