Message from discussion Problems returning a C char type to Fortran using ISO_C_BINDING
From: "FX" <coud...@alussinan.org>
Subject: Re: Problems returning a C char type to Fortran using ISO_C_BINDING
Date: Fri, 8 May 2009 16:20:38 +0000 (UTC)
Organization: Ecole Normale Superieure, Paris
Sender: coud...@quatramaran.ens.fr (FX)
Content-Type: text/plain; charset=iso-8859-1
X-Trace: nef.ens.fr 1241799638 10764 184.108.40.206 (8 May 2009 16:20:38 GMT)
NNTP-Posting-Date: Fri, 8 May 2009 16:20:38 +0000 (UTC)
X-Newsreader: Flrn (0.9.1 - 05/04)
> Am I missing something here?
C_CHAR returns a character kind number corresponding to the C "char"
type. Thus, it should only be used to declare CHARACTER variables, and
your second example (the one compiling with gfortran) is the right thing
to do. The fact that other compilers do not compile it is a bug in the
However, I see that gfortran throws an error when trying to compile the
first program. While, as I said, you should *not* write such a program, I
believe it's still valid, because you're lucky and the value of C_CHAR is
1, which happens to be a perfectly valid integer kind number. So, unless
I have missed a constraint in the standard, I think your first program
may be standard-conforming if you have the luck of C_CHAR matching a
valid integer kind (probably true for the most common compilers). Thus,
while gfortran is helping you avoid this nonportable code, I think a hard
error is overdoing it and a warning would be sufficient.
Now of course, in C a "char" is just an integer type. So, if you really
want to get that value as an integer in Fortran, you should use an
integer of the kind C_SIGNED_CHAR, which is designed for that purpose.
Hope this helps!