Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8949057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:09:01+00:00 2026-06-15T13:09:01+00:00

I recently came across a situation where I wanted to call a C function

  • 0

I recently came across a situation where I wanted to call a C function from Fortran, because of a useful snippet of C code. For convenience in array operations, I wanted to be able to call this function with array arguments as well, but it only accepted scalar arguments.

In Fortran, one can of course simply declare a procedure to be elemental to achieve this, and one can declare an interface to a C procedure with bind(C). However, since C does not have the concept of elemental procedures, the Fortran (2008) standard rules out this combination:

C1246 An elemental procedure shall not have the BIND attribute.

So, can this functionality be achieved in Fortran?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:09:02+00:00Added an answer on June 15, 2026 at 1:09 pm

    After some searching, I found that this is both possible and quite straightforward, using Fortran 2003 and the iso_c_binding intrinsic module; I thought I should document this here. An interface with the bind attribute can still be pure, so it can be referenced within a Fortran procedure which is itself elemental.

    The following is a short example with the C99 macro isnan, which returns a non-zero integer value if its argument is NaN. Of course, this can also be done for any user-defined C/C++ function that is free of side effects.

    elemental function isnan(val)
      use, intrinsic :: iso_c_binding
      implicit none
    
      real(c_double), intent(in) :: val
      logical(c_bool) :: isnan
    
      interface
        pure function isnan_C(val) bind(C, name = 'isnan')
          import
          ! Pass the parameter by value:
          real(c_double), value, intent(in) :: val
          integer(c_int) :: isnan_C
        end function
      end interface
    
      isnan = isnan_C(val) /= 0_c_int
    end function
    

    Example output using an array with some NaN values:

    program main
      use, intrinsic :: iso_c_binding
      implicit none
    
      real(c_double) :: nan(2,2) = 0.
      nan(:,2) = nan(1,1)/0.
    
      write(*,'(4F6.2 / 4L6)') nan, isnan(nan) ! Output: 0.00 0.00  NaN  NaN
                                               !            F    F    T    T
    end program
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently came across the following piece of code. It doesn't look valid because
I recently came across the situation where I wanted a set of classes to
We recently came across some sample code from a vendor for hashing a secret
Recently we came across a situation where we wanted to see the debug info
I recently came across a situation where I had to use XPath as part
Recently I came across a situation where set theory and set math fit what
I came across a situation recently. As any self respecting programmer would, when I
I came across some code recently that used a custom error handler to turn
I recently came across a piece of Java code with WeakReferences - I had
I recently came across a bit of not-well-tested legacy code for writing data that's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.