ctypes.c_uint is the ctypes fundamental type that maps to the C unsigned int. You use it to pass unsigned 32-bit integers to C functions, read them back, and declare unsigned fields in structures that mirror C layouts. It holds values from 0 to 4,294,967,295 on every mainstream platform, and it does not raise an error when you assign a value outside that range.
The ctypes module is part of the Python standard library, so no installation is needed. The examples below target Python 3.12 and later and run on Linux or macOS where they call into the C library.
ctypes.c_uint representsc_uint in structures and with byrefctypes.c_uint representsc_uint wraps the C type unsigned int. Although the C standard only guarantees at least 16 bits, every platform Python supports today uses 32 bits, which you can confirm with ctypes.sizeof.
The instance is a small mutable object that owns four bytes of memory. Read and write the Python integer through the .value attribute.
If you need exactly 32 bits regardless of platform, use the fixed-width alias ctypes.c_uint32. On current platforms it is the same type as c_uint, but the name documents the intent and matches uint32_t in C headers.
ctypes does no range checking on integer types. A value outside 0 to 4,294,967,295 is truncated to the low 32 bits.
This matches C unsigned arithmetic, which is modular, but it can hide bugs. If a value comes from untrusted input, validate it in Python before you wrap it:
Always set argtypes and restype on the function pointer. Without them ctypes assumes every argument is a C int and the return value is a signed int, so a large unsigned result would come back negative or truncated.
The following example calls htonl from the C library, which converts a 32-bit value from host to network byte order.
On a little-endian machine the four bytes are reversed. On Windows, load the library you need with ctypes.WinDLL or ctypes.CDLL and a library name instead of None.
c_uint in structures and with byrefC APIs often fill in an output parameter through a pointer. Pass ctypes.byref(obj) for that, and use c_uint for unsigned fields in a ctypes.Structure.
bytes(h) and from_buffer_copy let you serialise a structure to a binary format and parse it back. The byte order is the machine's native order, so for network protocols prefer ctypes.BigEndianStructure or the struct module.
| Problem | Cause | Fix |
|---|---|---|
| Large result appears negative | restype not set, so int is assumed | Set restype = ctypes.c_uint |
| Wrong value passed to C | argtypes not set | Declare argtypes for every function |
| Value silently changes | Out-of-range integer wraps | Validate the range in Python first |
| Wrong size on another platform | Assuming unsigned long is 32-bit | Use c_uint32 or c_uint explicitly |
| Crash when reading output | Passing the instance instead of a reference | Pass ctypes.byref(value) |
For plain binary parsing that never touches a C library, the struct module or int.from_bytes is simpler than ctypes.
c_uint always 32 bits?On all platforms CPython supports today, yes. If you need a guarantee, use ctypes.c_uint32.
c_uint and c_ulong?c_ulong maps to unsigned long, which is 8 bytes on 64-bit Linux and macOS but 4 bytes on Windows. c_uint is 4 bytes everywhere.
c_uint raise OverflowError for out-of-range values?No. ctypes truncates the value to the type's width without an error, so check the range yourself when it matters.
Read the .value attribute. Function calls with a restype of c_uint already return a plain int.
ctypes.c_uint is the bridge between Python integers and the C unsigned int. Set argtypes and restype on every foreign function, remember that out-of-range values wrap without warning, and prefer c_uint32 when the width must be exactly 32 bits. Use ctypes.Structure with c_uint fields to read and write binary layouts that match C headers.