How to print ( with printf ) complex number? For example, if I have this code:
#include <stdio.h> #include <complex.h> int main(void) { double complex dc1 = 3 + 2*I; double complex dc2 = 4 + 5*I; double complex result; result = dc1 + dc2; printf(" ??? \n", result); return 0; }
..what conversion specifiers ( or something else ) should I use instead "???"
The C programming language, as of C99, supports complex number math with the three built-in types double _Complex, float _Complex, and long double _Complex (see _Complex).
%d is a signed integer, while %u is an unsigned integer. Pointers (when treated as numbers) are usually non-negative. If you actually want to display a pointer, use the %p format specifier.
The %a formatting specifier is new in C99. It prints the floating-point number in hexadecimal form. This is not something you would use to present numbers to users, but it's very handy for under-the-hood/technical use cases.
printf("%f + i%f\n", creal(result), cimag(result));
I don't believe there's a specific format specifier for the C99 complex type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With