Software Archive
Read-only legacy content
17061 Discussions

Character comparison strangeness

Intel_C_Intel
Employee
584 Views
Hi

I am somewhat baffled by this behaviour. I have this program

PROGRAM LoadOfRubbish 
 
INTERFACE 
  SUBROUTINE IsAnything( NAME) 
    CHARACTER(LEN=*), OPTIONAL :: NAME 
  END SUBROUTINE 
END INTERFACE 
 
CALL IsAnything( NAME = '--------' ) 
CALL IsAnything( NAME = '        ' ) 
CALL IsAnything( NAME = '' ) 
 
END 
 
SUBROUTINE IsAnything ( NAME ) 
 
CHARACTER(LEN=*), OPTIONAL :: NAME 
 
IF(PRESENT(NAME)) THEN 
  WRITE(*,'(A)') NAME 
  WRITE(*,*) LEN(NAME) 
  IF(NAME .EQ. '') WRITE(*,*) 'WOOPS' 
END IF 
 
RETURN 
 
END 
 
When I run it I get the following output 
 
-------- 
 8 
         
 8 
 WOOPS 
 
 0 
 WOOPS 

It seems that the string ' ' (8 spaces) is being incorrectly
flagged as being equal to '' (NULL string)
I am completely baffled by this!!

Any insight would be most welcome

Geoff
0 Kudos
2 Replies
Steven_L_Intel1
Employee
584 Views
Standard Fortran behavior. When you compare strings of unequal lengths, the shorter string is padded on the right with blanks as necessary.

Steve
0 Kudos
Intel_C_Intel
Employee
584 Views
Steve

Thanks. Sorry to waste your time.

Geoff
0 Kudos
Reply