How can I work with complex numbers in C? I see there is a complex.h
header file, but it doesn't give me much information about how to use it. How to access real and imaginary parts in an efficient way? Is there native functions to get module and phase?

- 2,658
- 2
- 25
- 41

- 21,797
- 24
- 83
- 124
5 Answers
This code will help you, and it's fairly self-explanatory:
#include <stdio.h> /* Standard Library of Input and Output */
#include <complex.h> /* Standard Library of Complex Numbers */
int main() {
double complex z1 = 1.0 + 3.0 * I;
double complex z2 = 1.0 - 4.0 * I;
printf("Working with complex numbers:\n\v");
printf("Starting values: Z1 = %.2f + %.2fi\tZ2 = %.2f %+.2fi\n", creal(z1), cimag(z1), creal(z2), cimag(z2));
double complex sum = z1 + z2;
printf("The sum: Z1 + Z2 = %.2f %+.2fi\n", creal(sum), cimag(sum));
double complex difference = z1 - z2;
printf("The difference: Z1 - Z2 = %.2f %+.2fi\n", creal(difference), cimag(difference));
double complex product = z1 * z2;
printf("The product: Z1 x Z2 = %.2f %+.2fi\n", creal(product), cimag(product));
double complex quotient = z1 / z2;
printf("The quotient: Z1 / Z2 = %.2f %+.2fi\n", creal(quotient), cimag(quotient));
double complex conjugate = conj(z1);
printf("The conjugate of Z1 = %.2f %+.2fi\n", creal(conjugate), cimag(conjugate));
return 0;
}
with:
creal(z1)
: get the real part (for float crealf(z1)
, for long double creall(z1)
)
cimag(z1)
: get the imaginary part (for float cimagf(z1)
, for long double cimagl(z1)
)
Another important point to remember when working with complex numbers is that functions like cos()
, exp()
and sqrt()
must be replaced with their complex forms, e.g. ccos()
, cexp()
, csqrt()
.

- 4,245
- 25
- 44
-
12What is this `double complex`? Is this a language extension or some macro magic? – Calmarius Dec 16 '14 at 18:55
-
@Calmarius `complex` is a standard c99 type (under the hood on GCC, it is actually an alias to the _Complex type). – Snaipe Apr 12 '15 at 08:50
-
12@Snaipe: `complex` is not a type. It's a macro that expands to `_Complex`, which is a type *specifier*, but not a type by itself. The complex types are `float _Complex`, `double _Complex`, and `long double _Complex`. – Keith Thompson May 25 '15 at 08:22
-
4It's not just GCC, it's defined in the standard that _Complex is a type specifier and complex.h has a complex macro that expands to _Complex. Same goes for _Bool and stdbool.h. – jv110 Jul 13 '18 at 19:14
Complex types are in the C language since C99 standard (-std=c99
option of GCC). Some compilers may implement complex types even in more earlier modes, but this is non-standard and non-portable extension (e.g. IBM XL, GCC, may be intel,... ).
You can start from http://en.wikipedia.org/wiki/Complex.h - it gives a description of functions from complex.h
This manual http://pubs.opengroup.org/onlinepubs/009604499/basedefs/complex.h.html also gives some info about macros.
To declare a complex variable, use
double _Complex a; // use c* functions without suffix
or
float _Complex b; // use c*f functions - with f suffix
long double _Complex c; // use c*l functions - with l suffix
To give a value into complex, use _Complex_I
macro from complex.h
:
float _Complex d = 2.0f + 2.0f*_Complex_I;
(actually there can be some problems here with (0,-0i)
numbers and NaNs in single half of complex)
Module is cabs(a)
/cabsl(c)
/cabsf(b)
; Real part is creal(a)
, Imaginary is cimag(a)
. carg(a)
is for complex argument.
To directly access (read/write) real an imag part you may use this unportable GCC-extension:
__real__ a = 1.4;
__imag__ a = 2.0;
float b = __real__ a;

- 90,338
- 53
- 357
- 513
-
1almost every complex function will be implemented by compiler as builtin function in efficient way. Just use modern compiler and give it some non-zero level of optimization. – osgx Jun 21 '11 at 00:07
-
3FYI, since the OP mentions Python bindings, when working with Python I try to stick to C89 (since the rest of Python's code is C89, and if you want your extension to run on Windows, it's usually compiled with MVSC, which is limited to C89). I don't know that it's strictly necessary though. – detly Jul 09 '13 at 07:59
-
1The expression `(complex float) { r, i }` can also be used to set the separate parts of the number and independently (allowing the real part to be INF while the imaginary part is NAN, for instance). That avoids the GCC-specific keyword, though I'm not sure if it actually is portable. – cleong Apr 25 '14 at 19:36
-
2Note that Complex support is optional in C99: compilers may simply not have it if they define `__STDC_NO_COMPLEX__`. In practice however, it is implemented on major compilers. – Ciro Santilli OurBigBook.com Sep 17 '14 at 12:04
-
1Jasen, check page 182 of N1256 draft http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf#page=182 "7.3 Complex arithmetic
". Such keyword was probably selected in C99 to not break existing c (C90) programs which implements complex by hand. If – osgx Jan 25 '15 at 08:44is included, `complex` will be defined as macro, expanded to `_Complex`. You may be also interested in Derek M. Jones's "The New C Standard: An Economic and Cultural Commentary" (2008) page 500 "complex types" http://people.ece.cornell.edu/land/courses/ece4760/cbook1_1.pdf#page=502 -
@Jasen: Just about every new thing in C99 or C11 is implemented this way. `_Complex`, `_Bool`, `_Static_assert`, and `_Generic` are all perfectly standard C11 (or even C99, in the case of `_Complex` and `_Bool`). There are exceptions like `restrict` or `inline` in C99, which are just "plain" keywords. – Tim Čas Jan 28 '15 at 19:58
-
For convenience, one may include tgmath.h
library for the type generate macros. It creates the same function name as the double version for all type of variable. For example, For example, it defines a sqrt()
macro that expands to the sqrtf()
, sqrt()
, or sqrtl()
function, depending on the type of argument provided.
So one don't need to remember the corresponding function name for different type of variables!
#include <stdio.h>
#include <tgmath.h>//for the type generate macros.
#include <complex.h>//for easier declare complex variables and complex unit I
int main(void)
{
double complex z1=1./4.*M_PI+1./4.*M_PI*I;//M_PI is just pi=3.1415...
double complex z2, z3, z4, z5;
z2=exp(z1);
z3=sin(z1);
z4=sqrt(z1);
z5=log(z1);
printf("exp(z1)=%lf + %lf I\n", creal(z2),cimag(z2));
printf("sin(z1)=%lf + %lf I\n", creal(z3),cimag(z3));
printf("sqrt(z1)=%lf + %lf I\n", creal(z4),cimag(z4));
printf("log(z1)=%lf + %lf I\n", creal(z5),cimag(z5));
return 0;
}

- 11,217
- 6
- 43
- 49

- 4,245
- 5
- 30
- 50
The notion of complex numbers was introduced in mathematics, from the need of calculating negative quadratic roots. Complex number concept was taken by a variety of engineering fields.
Today that complex numbers are widely used in advanced engineering domains such as physics, electronics, mechanics, astronomy, etc...
Real and imaginary part, of a negative square root example:
#include <stdio.h>
#include <complex.h>
int main()
{
int negNum;
printf("Calculate negative square roots:\n"
"Enter negative number:");
scanf("%d", &negNum);
double complex negSqrt = csqrt(negNum);
double pReal = creal(negSqrt);
double pImag = cimag(negSqrt);
printf("\nReal part %f, imaginary part %f"
", for negative square root.(%d)",
pReal, pImag, negNum);
return 0;
}

- 587
- 5
- 25
To extract the real part of a complex-valued expression z
, use the notation as __real__ z
.
Similarly, use __imag__
attribute on the z
to extract the imaginary part.
For example;
__complex__ float z;
float r;
float i;
r = __real__ z;
i = __imag__ z;
r is the real part of the complex number "z" i is the imaginary part of the complex number "z"

- 108
- 5
-
5These are gcc-specific extensions. Another answer already mentioned them, and the [accepted answer](http://stackoverflow.com/a/9860772/827263) already how to do this in standard C. – Keith Thompson May 25 '15 at 08:25