-
December 23rd, 2012, 12:26 AM
#1
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.
********************
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.
-
December 23rd, 2012, 12:34 AM
#2
Re: C and Fortran mix programming problem
What does that print?
Regards,
Paul McKenzie
-
December 23rd, 2012, 12:37 AM
#3
Re: C and Fortran mix programming problem
Thank you Paul.
It prints '5' on the third row in the pic.
Best,
Hong
Originally Posted by Paul McKenzie
What does that print?
Regards,
Paul McKenzie
-
December 23rd, 2012, 12:44 AM
#4
Re: C and Fortran mix programming problem
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
-
December 23rd, 2012, 12:46 AM
#5
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?
-
December 23rd, 2012, 05:36 AM
#6
Re: C and Fortran mix programming problem
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
-
December 23rd, 2012, 08:04 AM
#7
Re: C and Fortran mix programming problem
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
-
December 23rd, 2012, 09:48 AM
#8
Re: C and Fortran mix programming problem
Thank you very much Paul.
Best,
Hong
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
-
December 25th, 2012, 12:43 AM
#9
Re: C and Fortran mix programming problem
I want to ask where it is from
-
December 25th, 2012, 01:11 AM
#10
Re: C and Fortran mix programming problem
Do you mean the zero? I do not know neither.
Originally Posted by cindyjohn
I want to ask where it is from
-
December 25th, 2012, 03:25 AM
#11
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.
-
December 25th, 2012, 10:17 AM
#12
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
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|