ctypes.c_complex128 does not exist in Python. The ctypes module has never had a type with that name, and the same is true for c_complex. Since Python 3.14, the supported way to pass a 128-bit complex number (two 64-bit doubles) to C is ctypes.c_double_complex, which matches the C99 type double _Complex.
Many older tutorials, including earlier versions of this page, describe c_complex or c_complex128 as if they were real. They are not, and code that uses them raises AttributeError. This article shows what to use instead on each Python version.
Python 3.14 added three complex types to ctypes, one for each C99 complex type. They are only defined when the platform and the Python build support C99 complex numbers.
| ctypes type | C type | Size on common platforms | Type code |
|---|---|---|---|
ctypes.c_float_complex | float _Complex | 8 bytes | F |
ctypes.c_double_complex | double _Complex | 16 bytes | D |
ctypes.c_longdouble_complex | long double _Complex | 16 or 32 bytes, depends on the platform | G |
The name c_complex128 comes from NumPy, where numpy.complex128 means two 64-bit floats. In ctypes the equivalent is c_double_complex. A 64-bit complex number (complex64 in NumPy) maps to c_float_complex.
On Python 3.13 and older, none of these types exist. You can check at runtime:
The value is exchanged as a normal Python complex. You pass a complex to the constructor and read it back through .value.
The object holds C memory with a real and an imaginary double. Reading .value creates a new Python complex each time. Arithmetic is done on the Python complex values, not on the ctypes object itself.
Floating-point rounding applies as usual, so the imaginary part can print as 0.8999999999999999 or similar. Compare with cmath.isclose() instead of ==.
C99 provides complex math functions in <complex.h>, which live in the math library. Set argtypes and restype with the complex types and call them directly on Python 3.14 or newer.
cabs returns the magnitude of the complex number, and csqrt returns its principal square root. On Windows, the Microsoft C runtime does not use the C99 complex ABI in the same way, so test your target platform before depending on this.
Before Python 3.14, you must pass complex numbers as two separate doubles or as a structure. Both are plain C types that ctypes supports on every version.
If you control the C code, the simplest approach is a function that takes two doubles:
For arrays or by-value structs, describe the layout yourself. A C99 double _Complex has the same memory layout as two consecutive doubles, so this works for pointers and arrays:
Do not pass such a structure by value to a function declared with a C99 complex parameter. The calling convention can differ from that of a struct on some platforms, so use pointers for that case.
For bulk numeric work, NumPy arrays of numpy.complex128 are usually better than raw ctypes. You can pass one to C with array.ctypes.data_as(...).
No. There is no c_complex and no c_complex128. Use c_double_complex on Python 3.14 or newer, or a two-double structure on older versions.
c_float_complex stores two 32-bit floats (8 bytes), and c_double_complex stores two 64-bit floats (16 bytes). Pick the one that matches the C type in the library you call.
Read the .value attribute. It returns a normal complex object.
Only where Python was built with C99 complex support. Check with hasattr(ctypes, "c_double_complex") and test your target operating system.
ctypes.c_complex128 and ctypes.c_complex are not part of Python. For 128-bit complex numbers use ctypes.c_double_complex on Python 3.14 or newer, and a structure of two c_double fields on older versions. Always set argtypes and restype, and prefer NumPy for large arrays.