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

ID 767251
Date 6/24/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 passing a string to a C function, the Fortran program must null-terminate the string.

Return Character Types from C to Fortran

The following examples show Fortran code that declares an interface to a C function, then calls the C function. The char pointer returned by the C function can be passed to the intrinsic subroutine C_F_STRPOINTER to convert the null terminated string to a Fortran character pointer. Fortran cannot use a C_PTR to a char* as is, since Fortran does not know the string's length.

Fortran code example:


! This Fortran program calls a C function to convert a binary 
! number to a character string containing the base 10 representation
! of its value. The null terminated C string result is then converted
! to a Fortran character pointer and printed.  
PROGRAM f_calls_c_returns_cptr
  USE, INTRINSIC :: ISO_C_BINDING,ONLY : C_PTR, C_INT, C_F_STRPOINTER, C_CHAR
  ! Interface to C function
  INTERFACE 
    TYPE (C_PTR) FUNCTION my_c_func (input) BIND(C)
      IMPORT, ONLY : C_PTR, C_INT
      INTEGER(C_INT),VALUE  :: input
    END FUNCTION my_c_func
  END INTERFACE
  
  CHARACTER(LEN=:, C_CHAR),POINTER :: f_str_ptr
  INTEGER(C_INT)                   :: bin_str = B'1101001110'

  ! This converts a C char* to a Fortran character pointer with maximum
  ! length of 1000 characters.
  CALL C_F_STRPOINTER (my_c_func (bin_str), f_str_ptr, NCHARS=1000))
  PRINT *, f_str_ptr
END PROGRAM
 

The call to C_F_STRPOINTER copies characters from the result of my_c_func to the target of f_str_ptr until it finds a NULL character, or the value specified for NCHARS is reached. LEN (f_str_ptr) is either the number of characters preceding the null terminator in the string returned by my_c_func, or 1000, whichever is less.

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;
}

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. The function F_C_STRING takes a Fortran character expression as an argument and returns a C string pointer.

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 C null terminated 
! string that can be printed by the caller.
FUNCTION get_fortran_words () BIND(C, NAME="GetFortranWords")
  USE,INTRINSIC  :: ISO_C_BINDING, ONLY : F_C_STRING, C_PTR
  TYPE(C_PTR)    :: get_fortran_words
  Get_fortran_words = F_C_STRING ("I like to type words!")
  RETURN
END FUNCTION get_fortran_words