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
  1. name ← Ada (padded to len=20)

    4integer :: name_length5name = "Ada"6name_length = len_trim(name)
    values this stepAda (padded to len=20)name
  2. name_length ← 3

    5name = "Ada"6name_length = len_trim(name)7print '(I0)', name_length
    values this step3name_lengthAda (padded)name
  3. print '(I0)', name_length

    6    name_length = len_trim(name)7    print '(I0)', name_length8end program character_strings
    output3
    values this step3name_length

Follow the Values

  1. name stores Ada in a character(len=20) variable.
  2. The stored text is padded with blanks up to length 20.
  3. len_trim(name) ignores the trailing blanks.
  4. name_length becomes 3, so the program prints 3. | value | what it means | | --- | --- | | Ada | visible letters in name | | 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.