1 Attachment(s)
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.
Attachment 30765
********************
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.
Re: C and Fortran mix programming problem
What does that print?
Regards,
Paul McKenzie
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
What does that print?
Regards,
Paul McKenzie
Re: C and Fortran mix programming problem
Quote:
Originally Posted by
taurushong
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
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?
Re: C and Fortran mix programming problem
Quote:
Originally Posted by
taurushong
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
Re: C and Fortran mix programming problem
Quote:
Originally Posted by
taurushong
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
Re: C and Fortran mix programming problem
Thank you very much Paul.
Best,
Hong
Quote:
Originally Posted by
Paul McKenzie
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
Re: C and Fortran mix programming problem
I want to ask where it is from
Re: C and Fortran mix programming problem
Do you mean the zero? I do not know neither.
Quote:
Originally Posted by
cindyjohn
I want to ask where it is from
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.
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
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.