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.
********************
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
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.
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.
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.
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
Originally Posted by nuzzle
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.
Bookmarks