Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Problems returning a C char type to Fortran using ISO_C_BINDING

Path: g2news2.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!u-picardie.fr!jussieu.fr!ens.fr!not-for-mail
From: "FX" <coud...@alussinan.org>
Newsgroups: comp.lang.fortran
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
Lines: 26
Sender: coud...@quatramaran.ens.fr (FX)
Message-ID: <gu1m4m$agc$1@nef.ens.fr>
References: <9d89dd0e-e843-43ef-852e-34509a4213ca@o27g2000vbd.googlegroups.com>
NNTP-Posting-Host: quatramaran.ens.fr
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: nef.ens.fr 1241799638 10764 129.199.129.64 (8 May 2009 16:20:38 GMT)
X-Complaints-To: abuse@ens.fr
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
compilers.

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!

-- 
FX