Nvfortran iso_c_binding with single char

I have a need to pass a single char by value from C function to Fortran caller. This method works on other vendors’ compilers but not on nvfortran from at least 23.5 to 24.1 (probably never worked):

lib.c

char one(){
 return '1';
}

main.f90

program c

use, intrinsic :: iso_c_binding

implicit none

interface
character(kind=c_char) function one() bind(C)
import
end function
end interface

character :: fone

fone = one()

print '(i0,1x,a)', len_trim(fone), fone

end program

This Fortran program prints random values for fone. I am guessing the C_CHAR is being handled as a pointer instead of value.

Here is a workaround using cast to/from C_INT that does work, but is only required for nvfortran. Other compilers don’t need this:

lib.c

char three(){
 return '3';
}

int c2i(){
    return (int) three();
}

main.f90

program test

use, intrinsic :: iso_c_binding

implicit none

interface
character(kind=c_char) function three() bind(C)
import
end function

integer(kind=c_int) function c2i() bind(C)
import
end function
end interface

character :: fc
integer :: e
character :: ef

fc = three()
e = c2i()
ef = achar(e)

print '(a)', fc

print '(a,1x,i0)', 'ascii code', e
print '(a)', ef

end program

This workaround may not strictly be correct (general limitations across platforms of blindly casting char to int), so in my actual program, I have worked around by duplicating the C function algorithm into Fortran without trying to pass the char value from C to Fortran.

Thanks for the report. I added TPR #35294 and sent it to engineering for review.