PRB: LEN_TRIM Appears to Return Incorrect String Length

Last reviewed: December 11, 1995
Article ID: Q89131
The information in this article applies to:
  • Microsoft FORTRAN for MS-DOS, versions 5.0 and 5.1
  • Microsoft FORTRAN for OS/2, versions 5.0 and 5.1
  • Microsoft FORTRAN PowerStation for MS-DOS, versions 1.0 and 1.0a
  • Microsoft FORTRAN PowerStation 32 for Windows NT, version 1.0 and 4.0

SYMPTOMS

LEN_TRIM may appear to return an incorrect trimmed string length. This occurs when the string in the calling routine is not initialized and the subprogram's formal argument declares the character string smaller than the original string's length.

CAUSE

All bytes in uninitialized strings are set to zero (null characters). When a string is initialized, any unused bytes are padded with spaces to the end of the string. The padding is done to the size that is declared in the routine in which the string is initialized. If a string is passed to a routine that declares the string size to be smaller then it was defined, the string will be incompletely padded with spaces. Upon return to the routine where the string was declared, the string will still have null characters at the end. The LEN_TRIM intrinsic function only parses for spaces and will stop immediately when it detects the final null character. This will appear to be incorrect because most editors display null characters as blanks.

RESOLUTION

LEN_TRIM works correctly if the string lengths are declared equal in all routines that the use the string. Also, initializing the string to all spaces in the routine in which it is originally declared will also correct the problem.

MORE INFORMATION

Code to Reproduce Problem

      character work*80
      integer len

      call getstr(work)
      len = LEN_TRIM(work)
      print *,len             ! len will be 80
      end

      subroutine getstr(outstr)
      character outstr*45
      outstr = 'This is a test'
      return
      end


Code to Correct Problem

      character work*80
      integer len

      work = ' '
      call getstr(work)
      len = LEN_TRIM(work)
      print *,len             ! len will be 14
      end

      subroutine getstr(outstr)
      character outstr*45
      outstr = 'This is a test'
      return
      end


Additional reference words: 4.00 5.00 5.10 1.00
KBCategory: kbprg kbcode kbprb
KBSubcategory: FORTLngIss


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: December 11, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.