Put simply, neither the POSIX specification of printf()
nor the Standard C specification includes any conversion formats for printing complex numbers. You'll have to pass the real and imaginary parts of a complex number separately to printf()
, and provide 2 conversion specifiers. For example, you could use cimag()
and creal()
from <complex.h>
and the generic format %g
(the +
ensures there's always a sign, +
or -
):
printf("%g%+gi", creal(array_ptr[0]), cimag(array_ptr[0]));
I would probably create myself a function to handle the formatting of complex numbers, and use that everywhere, rather than repeatedly writing the code out.
From a comment:
… my concern is not related to the printing part of the complex number; it is about why the C code can not print the second number (4.4) in the array and only the first one is printed (2.5).
See §6.2.5 Types ¶11ff:
¶11 There are three complex types, designated as float _Complex
, double _Complex
, and long double _Complex
.43) (Complex types are a conditional feature that implementations need not support; see 6.10.8.3.) The real floating and complex types are collectively called the floating types.
¶12 For each floating type there is a corresponding real type, which is always a real floating type. For real floating types, it is the same type. For complex types, it is the type given by deleting the keyword _Complex
from the type name.
¶13 Each complex type has the same representation and alignment requirements as an array type containing exactly two elements of the corresponding real type; the first element is equal to the real part, and the second element to the imaginary part, of the complex number.
So, the C complex numbers are treated as an array of two values. In the C code, array_ptr[0]
is one complex number. You are supposed to use creal()
and cimag()
or similar functions to get the two parts out of the one number. AFAICS, your Fortran function (I only know Fortran 77 — I never got to play with Fortran 90, and the code shown is radically different from F77!) is only setting a single complex number. So I don't believe there is an array_ptr[1]
for you to access; you get undefined behaviour. I'm not sure I have a good explanation of why you're getting anything sensible. I'd expect that array_ptr[0]
is equivalent to a pointer, and the code wouldn't print the number it points at.
I do have gfortran
(from GCC 8.3.0); I can try your code later.
I'm having nasty thoughts about what's going wrong — I suspect that you need a minor variant of this:
double _Complex value;
F_sub(&a, &value);
printf("Values are: %f %f and a= %d\n", creal(value), cimag(value), a);
If this is correct, then you're getting away with blue murder (or undefined behaviour), and are unlucky that the code isn't crashing. There is also a chance that you don't need the &
in &value
in the call to F_sub()
.
I also suspect that the free(array_ptr)
in your code is erroneous — I don't see why Fortran would allocate space.