ctypes.c_bool is the ctypes data type that represents the C _Bool type (bool in C99 and later, via stdbool.h). You create it from any Python object, read the result through its .value attribute as a Python bool, and use it to declare boolean arguments, return values, structure fields and arrays when calling C code from Python.
It is a fundamental type in the standard library ctypes module, so it needs no installation and works on Python 3.12 and later exactly as on older releases.
C has no boolean in the original C89 standard, so programs used int with 0 and 1. Since C99 the language has the built-in _Bool type, and <stdbool.h> provides the macros bool, true and false. In C23, bool, true and false are keywords. c_bool maps to this type, which is one byte on all mainstream platforms.
| Python side | C side |
|---|---|
ctypes.c_bool | _Bool / bool |
True | 1 |
False | 0 |
.value | a Python bool |
Because c_bool is a ctypes type, an instance owns a small block of memory that you can pass to C by reference with ctypes.byref() or ctypes.pointer().
The constructor accepts any Python object and stores its truth value, so c_bool(5) holds True and c_bool("") holds False.
Note that c_bool stores a normalised 0 or 1. You always get a real bool back from .value, never an integer.
By default ctypes assumes every argument and the return value is a C int. For a function that uses bool you must declare argtypes and restype, otherwise the value may be passed or read incorrectly, especially on platforms where the calling convention treats small types specially.
Assume this C code is compiled into libflags.so (or flags.dll on Windows):
With restype = ctypes.c_bool ctypes converts the returned value to a Python bool automatically, so you do not need to call .value on it. Setting argtypes also lets ctypes convert Python values and reject wrong types with a clear ArgumentError.
c_bool can be a field in a ctypes.Structure, which is the usual way to mirror a C struct that contains flags.
Fields are laid out following the platform's C alignment rules, so the structure size is usually larger than the sum of the field sizes. For an array of flags, multiply the type by a length:
When a C function writes a boolean through a pointer, create a c_bool and pass it with byref().
ctypes.byref(result) is cheaper than ctypes.pointer(result) because it does not build a full pointer object. Use pointer() only when you need to store or dereference the pointer in Python.
restype. Without it ctypes reads the return value as int, and a bool return in a register may contain garbage in the upper bits.c_int for a C bool. The sizes differ (4 bytes against 1 byte), which corrupts structure layouts and pointer writes..value of a c_bool with is True. This works, but if flag.value: is clearer and more Pythonic.c_bool with the Windows BOOL type. The Win32 BOOL is a 32-bit int, available as ctypes.wintypes.BOOL, and a different type from the one-byte C99 bool.c_bool between threads without a lock. Reads and writes of the stored value are not synchronised for you.It is one byte on mainstream platforms, equal to ctypes.sizeof(ctypes.c_bool). That matches the C _Bool type used by current compilers.
It stores True. The constructor evaluates the truth value of the object, and .value returns a Python bool.
Use c_bool for a C99 bool. Use c_int only when the C function genuinely returns an int, as many older C APIs and the Win32 BOOL type do.
No. c_bool is a ctypes wrapper that owns C-compatible memory. The .value attribute converts it to the normal Python bool.
ctypes.c_bool is the ctypes counterpart of the C _Bool type. Create it with c_bool(True), read it with .value, and always set argtypes and restype when calling C functions that use booleans. Use it for structure fields, arrays and output parameters passed with byref(), and do not confuse it with the 32-bit Windows BOOL.