Passing fortran variables to C in mixed programming

Passing fortran variables to C in mixed programming


I've a subroutine that's written in C. My main program and everything else are in fortran 90. I have a global data module which consist of some variables and it can be assessed globally ie

module global_data

implicit none


real :: dd(10)

end module global_data

I would like to pass the values of a variable (e.g. dd) to a C subroutine, do some calculation, and out comes the result of the variable.

How can I do this? Is there a sample code somewhere? I tried to find some info on the net but they seem quite complicated...

Btw, my environment is linux and it's using ifort and icc ver 9.1

Thanks in advance!

3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Just call the C routine and pass the variable as an argument. On the C side, be sure to declare the argument as *float or something similar. There is a chapter of the documentation on mixed-language programming which will be of interest. You'll want the C routine to have a lowercase name with a trailing underscore, as that is the convention used for Fortran routines on Linux.

Retired 12/31/2016

Since you're learning, you should consider the advantage of starting out with a Fortran compiler which supports the current standard method, ISO C interoperability. For example, you could install a pre-built Fortran from the gfortran wiki, or use ifort 10.1. As you point out, it could be confusing if you don't choose between current and legacy methods, which differ somewhat between linux, Windows, and various flavors of Unix.
If you mean that you want to use ifort 9.1, you should find examples in the documentation in the /doc/ directory of the installation. Both legacy linux style linkage and DEC directive methods are supported.

Leave a Comment

Please sign in to add a comment. Not a member? Join today