HP Fortran Programmer's Guide (B3908-90031; September 2011)
Calling C routines from HP Fortran
C strings
Chapter 8 197
20 FORMAT(/, 'The names passed back to Fortran: ', A, 1X, A)
END PROGRAM main
Example 8-8 get_string.c
#include <stdio.h>
#include <string.h>
void fix_string_for_f90(char s[], int len);
/* get_string: overwrites the string arguments fname and lname;
* fname_len and lname_len are the hidden length arguments, which
* are implicitly passed by Fortran with each string argument.
*/
void get_string(char fname[], char lname[], int fname_len,
int lname_len)
{
printf(“The names passed to C: %s %s\n", fname, lname);
printf(“\nEnter the first and last names of a banjo player: ");
scanf(“%s%s”, fname, lname);
fix_string_for_f90(fname, fname_len);
fix_string_for_f90(lname, lname_len);
}
/* fix_string_for_f90: replaces the null at the end of the string
* in the character array and th a blank and blank fills the
* remaining elements up to len; this processing is necessary if
* the character variable is to be manipulated by Fortran
*/
void fix_string_for_f90(char s[], int len)
{
int i;
for (i = strlen(s); i < len; i++)
s[i] = ' ';
}
Below are the command lines to compile, link, and execute the program, followed by the output from a
sample run.
$ cc -Aa -c get_string.c
$ f90 pass_chars.f90 get_string.o
$ a.out
The names passed to C: Pete Seeger
Enter the first and last names of a banjo player: Wade Ward