PRB: LEN_TRIM Appears to Return Incorrect String Length

This article was previously published under Q89131
This article has been archived. It is offered "as is" and will no longer be updated.
SYMPTOMS
LEN_TRIM may appear to return an incorrect trimmed string length. Thisoccurs when the string in the calling routine is not initialized and thesubprogram's formal argument declares the character string smaller than theoriginal string's length.
CAUSE
All bytes in uninitialized strings are set to zero (null characters). Whena string is initialized, any unused bytes are padded with spaces to the endof the string. The padding is done to the size that is declared in theroutine in which the string is initialized. If a string is passed to aroutine that declares the string size to be smaller then it was defined,the string will be incompletely padded with spaces. Upon return to theroutine where the string was declared, the string will still have nullcharacters at the end. The LEN_TRIM intrinsic function only parses forspaces and will stop immediately when it detects the final null character.This will appear to be incorrect because most editors display nullcharacters as blanks.
RESOLUTION
LEN_TRIM works correctly if the string lengths are declared equal in allroutines that the use the string. Also, initializing the string to allspaces in the routine in which it is originally declared will also correctthe 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				
4.00 5.00 5.10 1.00
Properties

Article ID: 89131 - Last Review: 10/14/2013 17:49:59 - Revision: 2.0

  • Microsoft FORTRAN Compiler 5.0
  • Microsoft FORTRAN Compiler 5.1
  • Microsoft FORTRAN Compiler 5.0
  • Microsoft FORTRAN Compiler 5.1
  • Microsoft FORTRAN PowerStation 1.0 Standard Edition
  • Microsoft Fortran PowerStation 1.0a for MS-DOS
  • Microsoft FORTRAN PowerStation 32
  • Microsoft FORTRAN PowerStation 32
  • kbnosurvey kbarchive kblangfortran KB89131
Feedback