Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C complex number and printf

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 "???"

like image 744
gameboy Avatar asked Nov 04 '10 17:11

gameboy


People also ask

Does C support complex numbers?

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).

What is %U and %D in C?

%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.

What is %A in printf?

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.


1 Answers

printf("%f + i%f\n", creal(result), cimag(result)); 

I don't believe there's a specific format specifier for the C99 complex type.

like image 96
John Calsbeek Avatar answered Sep 19 '22 20:09

John Calsbeek