CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Dec 2012
    Posts
    6

    C and Fortran mix programming problem

    Hi all, I need to call a function written in Fortran from C for recent work. Because I have no such experience before, I start with a toy program as follows.

    However when it runs, there is a '0' before 'END', as in the following pic. I want to ask where it is from.
    Name:  Screen Shot 2012-12-22 at 9.20.29 PM.png
Views: 1135
Size:  11.6 KB

    ********************
    The C code is
    #include <stdio.h>
    #include <math.h>
    extern int add_(int * ,int *);

    int main()
    {
    int a,b;
    int z;
    a=2;
    b=3;
    printf("a=%d,b=%d",a,b);
    z=add_(&a,&b);
    printf("%d\n",z);
    printf("END\n");
    }

    ************************
    The fortran code is

    INTEGER FUNCTION ADD (A, B)
    INTEGER A, B
    INTEGER C
    WRITE (*,*) 'IN FORTRAN'
    WRITE (*,*) 'A=',A,'B=',B
    C=A+B
    WRITE (*,*) 'FINISH ADD',C
    RETURN
    END

    ***********************

    Thanks guys.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C and Fortran mix programming problem

    Code:
    printf("%d\n",z);
    What does that print?

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Dec 2012
    Posts
    6

    Re: C and Fortran mix programming problem

    Thank you Paul.

    It prints '5' on the third row in the pic.

    Best,
    Hong

    Quote Originally Posted by Paul McKenzie View Post
    Code:
    printf("%d\n",z);
    What does that print?

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C and Fortran mix programming problem

    Quote Originally Posted by taurushong View Post
    It prints '5' on the third row in the pic.
    Really?

    Follow your code:
    Code:
    WRITE (*,*) 'FINISH ADD',C
    So what does this line do? Isn't the C the value that is printed, which is 5?

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Dec 2012
    Posts
    6

    Re: C and Fortran mix programming problem

    You are right. '5' is from the value of C. Then I am really confused. Why z=0?

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: C and Fortran mix programming problem

    Quote Originally Posted by taurushong View Post
    You are right. '5' is from the value of C. Then I am really confused. Why z=0?
    I suppose it's because the ADD function doesn't know you want to return C.

    If I recall correctly in Fortran you're supposed to assign the wanted return value to the function name like,

    ADD = C
    RETURN

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C and Fortran mix programming problem

    Quote Originally Posted by taurushong View Post
    You are right. '5' is from the value of C. Then I am really confused. Why z=0?
    You have a variable in your 'C' program called "z", and you're printing it after assigning it the return value of that function. If that function returns 0, then z is 0. That's the only explanation from the C++ side of things.

    Why the function returns 0 is a FORTRAN issue, or an issue with how FORTRAN returns values to a 'C' program, and I have no idea how that works.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Dec 2012
    Posts
    6

    Re: C and Fortran mix programming problem

    Thank you very much Paul.

    Best,
    Hong

    Quote Originally Posted by Paul McKenzie View Post
    You have a variable in your 'C' program called "z", and you're printing it after assigning it the return value of that function. If that function returns 0, then z is 0. That's the only explanation from the C++ side of things.

    Why the function returns 0 is a FORTRAN issue, or an issue with how FORTRAN returns values to a 'C' program, and I have no idea how that works.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Dec 2012
    Posts
    1

    Re: C and Fortran mix programming problem

    I want to ask where it is from

  10. #10
    Join Date
    Dec 2012
    Posts
    6

    Re: C and Fortran mix programming problem

    Do you mean the zero? I do not know neither.

    Quote Originally Posted by cindyjohn View Post
    I want to ask where it is from

  11. #11
    Join Date
    May 2009
    Posts
    2,413

    Re: C and Fortran mix programming problem

    As I said in my previous post the ADD function isn't assigned a return value. If that's not an error the function must return something and 0 sounds reasonable.

  12. #12
    Join Date
    Dec 2012
    Posts
    6

    Re: C and Fortran mix programming problem

    Thank you very much for your answer. I don't know why I missed your previous reply. Now the program returns 5. Thank you for your help.

    Wish you a merry Christmas and a happy new year.

    Hong

    Quote Originally Posted by nuzzle View Post
    As I said in my previous post the ADD function isn't assigned a return value. If that's not an error the function must return something and 0 sounds reasonable.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured