ctypes.c_char is not a module. It is a class in Python's ctypes module that represents a C char, a single byte. You use it in argtypes, restype and structures when you call C code through a shared library. In Python it holds a bytes object of length 1, not a str.
For whole strings, use ctypes.c_char_p, ctypes.create_string_buffer() or ctypes.c_wchar_p. For small integers use c_byte or c_ubyte.
Note: this question is about Python, although it was filed under HTML; the category should be Python.
c_char representscharc_char in structures and arraysc_char representsIn C, char is a one-byte type, usually holding an ASCII or UTF-8 byte. c_char mirrors it. You create one from a single-byte bytes value, and read it back with .value:
Passing a str such as "A" raises TypeError, because ctypes needs bytes. Passing more than one byte also fails.
charSuppose you compile this C file:
Build it as a shared library on Linux with gcc -shared -fPIC -o libdemo.so demo.c. Then call it:
Always set argtypes and restype. Without them ctypes assumes int for arguments and the return value, which gives wrong results or crashes on 64-bit systems. When restype is c_char, ctypes returns a one-byte bytes object. On Windows, load the library with ctypes.CDLL("demo.dll") or ctypes.WinDLL.
The original example loaded a C file as if it were Python; C code must be compiled first and called through the resulting library.
c_char in structures and arraysc_char is common as a fixed-size buffer inside a Structure, where a multiplication creates an array:
A c_char array behaves like a byte string: .value stops at the first NUL byte and .raw returns all bytes. The size shown depends on alignment rules, which are typically 20 bytes here.
When C fills a buffer, use create_string_buffer(), which creates a mutable array of c_char:
buf.value returns bytes up to the first NUL, while buf.raw returns the full buffer. Variadic C functions such as snprintf work on most platforms, but their calling convention can differ on some, such as Apple Silicon, so prefer fixed-signature functions in portable code.
An array of c_char can be indexed. Each element is a length-one bytes value:
| ctypes type | C type | Python value | Use for |
|---|---|---|---|
c_char | char | bytes of length 1 | A single byte or character |
c_char_p | char * | bytes or None | NUL-terminated byte string |
create_string_buffer(n) | char[n] | Mutable buffer | C functions that write into memory |
c_wchar | wchar_t | str of length 1 | Wide character |
c_byte / c_ubyte | signed / unsigned char | int | Small integers |
The right choice depends on meaning. If C treats the value as a number, use c_ubyte. If it is text, use c_char.
"é".encode("utf-8") produces several bytes, which do not fit a single c_char.cffi, Cython or pybind11, which offer more checking than ctypes.ctypes.c_char a module?No. It is a class (a fundamental data type) provided by the ctypes module.
c_char to a Python string?Take .value to get bytes, then decode it: ch.value.decode("ascii").
c_char and c_char_p?c_char is one byte. c_char_p is a pointer to a NUL-terminated byte string.
TypeError with a str?ctypes expects bytes for char types. Use b"A" or "A".encode().
ctypes.c_char represents one C char as a one-byte bytes value. Declare argtypes and restype, use c_char_p or string buffers for text, and use c_char arrays inside structures for fixed-size fields.