C _Complex Keyword
Example
Create a complex number and print its two parts:
double _Complex myNum = 3.0 + 4.0 * I;
printf("Real part: %.1f\n", creal(myNum));
printf("Imaginary part: %.1f", cimag(myNum));
Definition and Usage
The _Complex keyword declares a complex number type. A complex number has two parts: a real part and an imaginary part. Complex numbers are used in math, science and engineering.
Use it together with float, double or long double, like double _Complex. The <complex.h> header gives you I (the imaginary unit, the square root of -1), and the functions creal() and cimag(), which return the real part and the imaginary part.
If you include <complex.h>, you can also write complex instead of _Complex, like double complex myNum;.
Related Pages
Read more about math functions in our C Math Tutorial.
Read more about data types in our C Data Types Tutorial.