What is the _Complex keyword in C?

We use _Complex to declare a complex number in C.

It is an expansion of the complex macro in <complex.h>

A complex number is written as the sum of one real number and one real number multiplied by the imaginary unit, I.

A Complex Number

_Complex types

The following are the three _Complex types:

  1. float _Complex
  2. double _Complex
  3. long double _Complex

By including the <complex.h> header, we can replace the _Complex keyword with complex and have the same effect.

Code

#include <complex.h>
#include <stdio.h>
int main(void)
{
//declaring a complex number using complex
double complex z1 = 1 + 2*I;
//Arithmetic operation
z1 = 2 * z1;
//Printing correct to 1dp
printf("2 * (1.0+2.0i) = %.1f%+.1fi\n", creal(z1), cimag(z1));
//declaring a complex number using _Complex
double _Complex z2 = 3 + 4*I;
//Arithmetic operation
z2 = 1 / z2;
//Printing correct to 1dp
printf("1 / (3.0+4.0i) = %.1f%+.1fi\n", creal(z2), cimag(z2));
}

In the above example, we declare two complex numbers.

To define the imaginary part of the number, we multiply a real number with I.

Next, we perform a mathematical operation on the complex numbers.

Finally, the results are printed.

creal() and cimag() are defined in the <complex.h> header. We use these to extract the real and imaginary parts of a complex number, respectively.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved