g77 ichar signed?

MASUDA Kooiti masuda@frontier.esto.or.jp
Mon Nov 19 16:27:00 GMT 2001


To g77 developpers:

I am a Fortran programmer using 'g77' on several installations of Linux.
It is very useful software, and it is generally very portable.

However, I recently found an unexpected behaviour of the intrinsic
function 'ichar' in some version of 'g77' (but not in others).

The problem is that 'ichar' returns signed integer values (-128..127)
if the argument is a character variable, while it returns unsigned
values (0..255) if the argument is a literal character.

The version is
> g77 version 2.96 20000731 (Red Hat Linux 7.0)
> (from FSF-g77 version 0.5.26 20000731 (Red Hat Linux 7.0))
I have browsed your WWW pages and searched for 'ichar' and 'signed',
and found nothing in particular.
I am sure that this is not the latest version of 'gcc', but please
forgive me that I am not able to build a later version of 'gcc' than
the Linux distribution provides.
Also it is difficult for me to follow strictly the guideline for
reporting 'gcc' bugs, which seems to be intended primary for
such bugs that crash the compiler.

The problem does not appear, at least, in the following version.
> g77 version egcs-2.91.66 19990314 (egcs-1.1.2 release)
> (from FSF-g77 version 0.5.24-19981002)
The returned values are always unsigned there.

For more information about the version of 'g77' with trouble,
the first and last parts of the output of 'g77 --verbose' is as follows
g77 version 2.96 20000731 (Red Hat Linux 7.0)
(from FSF-g77 version 0.5.26 20000731 (Red Hat Linux 7.0))
...
__G77_LIBF77_VERSION__: 0.5.26 20000731 (prerelease)
@(#)LIBF77 VERSION 19991115
__G77_LIBI77_VERSION__: 0.5.26 20000731 (prerelease)
@(#) LIBI77 VERSION pjw,dmg-mods 19991115
__G77_LIBU77_VERSION__: 0.5.26 20000731 (prerelease)
@(#) LIBU77 VERSION 19980709

I tested it with the following Fortran program
(between <pre> and </pre>).  In order to keep e-mail message in
7-bit code, I follow the convention of HTML.  The string 'ä'
is actually a one-byte character in Latin-1 (a-Umlaut in German).
<pre>
      CHARACTER h
*
      h = 'ä'
      WRITE(*,*) 'h = ',h
      WRITE(*,*) 'ichar(h) = ', ichar(h)
      WRITE(*,*) 'ichar(''ä'') = ', ichar('ä')
      END
</pre>

The operation is just as follows, and there is no error messages.
g77 ticharau.f
./a.out > ticharau.out

The output is as follows (again 'ä' is a character):
<pre>
 h = ä
 ichar(h) =  -28
 ichar('ä') =  228
</pre>

As I already know the problem, I can work around it by using such a
code as:
      INTEGER   iuchar(h)
      CHARACTER h
      INTEGER   ic
*
      ic = ichar(h)
      IF(ic .LT. 0) ic = ic + 256
      iuchar = ic
      RETURN
      END
But it looks ugly.

I am not sure whether the Fortran standard forces that the values
returned from 'ichar' should be unsigned or not.
However, it should be consistent anyway.

By the way, 'f2c' (from netlib.bell-labs.com) as of 22 Aug. 2001
returns unsigned values (228 in the both positions of the above example).
In the 'CHANGES' file of 'f2c', there is the following entry.
>Fri Sep 16 17:50:18 EDT 1994
...
>  Arrange for ichar() to treat characters as unsigned.
>  libf77: s_cmp.c: treat characters as unsigned in comparisons.
>These changes for unsignedness only matter for strings that contain
>non-ASCII characters.  Now ichar() should always be >= 0.
--
Kooiti MASUDA (D. Sc.)
Frontier Reserch System for Global Change
Showa, Kanazawa-ku, Yokohama 236-0001, Japan
masuda@frontier.esto.or.jp



More information about the Gcc-bugs mailing list