Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 3/22/2024
Public
Document Table of Contents

Return Character Data Types

A discrepancy occurs when working with C and Fortran character strings: C strings are null-terminated while Fortran strings have known lengths. A C routine that returns a character string returns a pointer to a null-terminated string. A Fortran routine does not know the string’s length.

If a Fortran routine is returning a string to a C program, the Fortran program must null-terminate the string.

Return Character Types from C to Fortran

The following examples show the Fortran code that declares interfaces to a C routine, then calls the C routine. The pointer returned by the C routine can be passed along to another C routine. Fortran cannot use this method as-is, since Fortran does not know the string's length.

Fortran code example:

! This program declares a C program that returns a char*
!  calls it, and has to pass that return value along to
!  a different c program because type(c_ptr) is limited
!  in a pure Fortran program.

program FCallsCReturnsCptr

use, intrinsic :: iso_c_binding

! declare the interfaces to the C routines

interface
   type(C_PTR)function my_croutine1 ( input) bind(c)
     import
     integer(c_int), value :: input
   end function my_croutine1

   subroutine my_cprint( string ) bind(c)
     import c_ptr
     type(C_PTR), value :: string
   end subroutine my_cprint
end interface

   call my_cprint(my_croutine1(42))

end program 

Called C routine example:

#include <stdio.h>

char *my_croutine1 (int input) {
   static char temp[30];
   temp[0] = '\0';
   sprintf(temp, "in routine, you said %d", input);
  return temp;
}

void my_cprint (char *string) {
  printf ("cprint says %s\n", string);
 return;
} 
         
      

The previous example shows the C code used to call a Fortran routine. The Fortran routine returns a string that is then printed by the C program. To pass a string back to C from Fortran is simple. The string can be easily used by the C program because it is null-terminated.

In the previous examples, the following restrictions and behaviors apply:

  • The function's length and result do not appear in the call statement; they are added by the compiler.

  • The called routine must copy the result string into the location specified by result; it must not copy more than length characters.

  • If fewer than length characters are returned, the return location should be padded on the right with blanks; Fortran does not use zeros to terminate strings.

  • The called procedure is type void.

Return Character Types from Fortran to C

The following examples show the C code used to call a Fortran routine; the Fortran routine returns a string that is then printed by the C program.

C code example:

#include <stdio.h>

char *GetFortranWords(void);

int main() {

   printf ("Fortran says this: %s\n", GetFortranWords());
   return 0;
} 
         
      

Called Fortran routine example:

! this routine is called from C, returning a string that
!  can be printed by the caller

function GetFortranWords () bind(c, name="GetFortranWords")
use, intrinsic :: iso_c_binding
type(C_ptr) :: GetFortranWords
character(len=30),save, target :: returnval

returnval = "I like to type words!" // char(0)


GetFortranWords = C_LOC(returnval)
return
end