The ctypes.c_bool type represents the C _Bool (or C++ bool) type, which is one byte wide and holds true or false. You use it when you call C functions that take or return booleans, and when you declare boolean fields in a ctypes.Structure. It is a type in the ctypes module, not a module itself.
On the Python side, c_bool converts to and from the normal True and False values, so you rarely need to think about the byte underneath.
c_bool is a ctypes simple type, like c_int or c_double. Each instance wraps one byte of memory. Reading .value always returns a real Python bool:
You may pass any Python object whose truth value is meaningful, and ctypes stores it as a boolean:
Compare this with c_int, where the value is stored as a number and can be any integer. c_bool normalises everything to true or false, which matches how C code treats a bool.
When a C function uses bool from stdbool.h, declare it explicitly. Without argtypes and restype, ctypes assumes int, which can read garbage from the upper bytes of a return register:
The library path and file name here are an example, so build a shared library from your own C source first. With restype = c_bool, ctypes returns a Python bool directly, and you do not have to call .value.
Structures often mix booleans with other types. Using c_bool keeps the layout identical to the C definition:
On most platforms sizeof(Config) is 12, not 6, because the compiler aligns the c_int on a four byte boundary and pads the end. ctypes follows the same alignment rules as the platform C compiler, which is the reason to use c_bool rather than a manual c_ubyte.
Create a fixed array of booleans by multiplying the type, and pass a pointer to a C function that fills it:
For output parameters, create a c_bool() and pass it with ctypes.byref() to a function declared with POINTER(c_bool).
When a C function writes a boolean through a bool * parameter, pass a c_bool with byref() and read .value after the call:
The commented lines show the call you would make with your own library. Because c_bool is one byte, a mismatched type such as c_int would let C overwrite three neighbouring bytes, so always declare the pointer type that matches the C signature.
| Type | C equivalent | Size | Notes |
|---|---|---|---|
c_bool | _Bool / bool | 1 byte | Value is a Python bool |
c_ubyte | unsigned char | 1 byte | Plain number 0 to 255 |
c_int | int | usually 4 bytes | Do not use for a C bool |
If a C API returns an int that happens to be 0 or 1, as many older C APIs do, use c_int and convert with bool(...) yourself. Use c_bool only when the C declaration really is bool.
Reading .value returns a Python True or False. As a function restype, ctypes returns a bool directly.
One byte on mainstream platforms, matching the C _Bool type.
Use c_int when the C signature says int, and convert with bool(). Use c_bool only when the C signature uses bool or _Bool.
No. It is a type exposed by the ctypes module.
ctypes.c_bool maps the C boolean to a one byte ctypes type that reads back as a Python bool. Declare it in argtypes, restype and structure _fields_ to match the C signature exactly, and prefer c_int for legacy APIs that use integers as booleans.