Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29277 Discussions

return -1 from Fortran to C and got 4294967295

lxh37
Beginner
1,108 Views

 

I have a function in Fortran:

integer(our_int) function samplesize( maxn, delta, conf, msgLen, iMsg) result( answer )
!DEC$ ATTRIBUTES DLLEXPORT, C :: samplesize
....

our_int is set to be: selected_int_kind(9)

It returns -1 when there is an error, but when I call it in C below, I got 4294967295 (2**32 -1). After I change

integer(our_int) to integer(8), it seems to fix the problem, I wonder why selected_int_kind(9) doesn't

work?

Thanks!

Liz

extern long samplesize(long maxn, double delta, double conf, long msgLen, int *iMsg);

#include <stdio.h>
#include <stdlib.h>

long Calculate(long maxn, double delta, double conf, char* errMsg)
{

    long answer;
 ...
    
    answer = samplesize(maxn, delta, conf, ERR_MSG_LENGTH, iarr);

...
    
    return answer;
}

 

0 Kudos
3 Replies
TimP
Honored Contributor III
1,108 Views

You would need selected_int_kind(10) or (preferably, with USE iso_c_binding) c_long, to get a 64-bit integer kind corresponding with long in 64-bit linux C.  The c_long selection would adjust automatically if you happened to change between targets where C uses 32-bit or 64-bit long.

0 Kudos
lxh37
Beginner
1,108 Views

 

Dear Tim:

Thanks! This is very helpful, I wonder in Mac, selected_real_kind(15,307) in FORTRAN is compatible with double in C?

How about for Windows (32-bit and 64-bit)? So far, we used selected_int_kind(9) for long in C and selected_real_kind(15,307) for

double in C and they worked in 32-bit and 64-bit Windows. We are developing plugins for both Windows and Macs, want to

make sure that we did everything right.

Thanks!

Liz

 

 

 

0 Kudos
TimP
Honored Contributor III
1,108 Views

lxh37 wrote:

 

Dear Tim:

Thanks! This is very helpful, I wonder in Mac, selected_real_kind(15,307) in FORTRAN is compatible with double in C?

How about for Windows (32-bit and 64-bit)? So far, we used selected_int_kind(9) for long in C and selected_real_kind(15,307) for

double in C and they worked in 32-bit and 64-bit Windows. We are developing plugins for both Windows and Macs, want to

make sure that we did everything right.

Thanks!

Liz

 

 

 

[/quote

On all platforms supported by Intel, C double is in fact compatible with Fortran selected_real_kind(15), which would the same as c_double.

The most confusing C compatibility issue is the one with long, where most 64-bit targets (all gcc ones) set long as int64_t, while 32-bit targets (and also Microsoft 64-bit compiler) set long as int32_t.   That makes a good case for using c_long.

0 Kudos
Reply