ctypes.c_longlong is the ctypes type for the C long long, a signed integer that is at least 64 bits wide and is exactly 64 bits on every mainstream platform. You use it to declare arguments, return values and structure fields when you call C libraries from Python. Its range is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807, and values outside that range wrap around silently instead of raising an error.
The sections below show how to create values, call a real C function, handle overflow and read binary data with it.
c_longlong belongs to the standard library ctypes module. Unlike c_long, whose size changes between Windows and Linux, long long is 64 bits wide on Windows, Linux and macOS, on both x86-64 and ARM64. In ctypes, c_longlong is also available under the fixed-width name c_int64 when the sizes match, so ctypes.c_int64 is the clearer choice when your C code uses int64_t.
| ctypes type | C type | Size (bytes) | Typical use |
|---|---|---|---|
c_short | short | 2 | 16-bit samples |
c_int | int | 4 | General integers |
c_long | long | 4 or 8 | Platform dependent |
c_longlong | long long | 8 | File sizes, timestamps, IDs |
c_ulonglong | unsigned long long | 8 | Unsigned 64-bit values |
You create an instance from a Python int and read it back through .value. The result is an ordinary Python int, which has no size limit, so the conversion back is lossless.
Instances are mutable. ctypes.byref(n) passes a pointer to that memory, which is how you let C code write into your variable.
ctypes does not check the range of an integer. It keeps the low 64 bits, exactly like a C cast:
Python integers never overflow, so this wrap-around only appears when a value passes through a ctypes type. If a number came from user input or a database, check its range yourself before you pass it to C.
The C standard library has llabs, which takes and returns a long long. Declare argtypes and restype so ctypes converts the value correctly. Without a declaration, ctypes assumes a C int for both, and large values are truncated to 32 bits.
CDLL(None) works on Linux and macOS. On Windows use ctypes.CDLL("ucrtbase") or ctypes.cdll.msvcrt. For your own library, load it with its path, for example ctypes.CDLL("./libmath64.so"), and declare each function the same way.
Multiply the type by a length to get an array type. The array is contiguous memory that C functions can read or fill.
As a structure field, c_longlong aligns to 8 bytes on most platforms:
The structure is 24 bytes, not 20, because the compiler pads the end to a multiple of 8.
To decode a value from bytes, use from_buffer_copy. It copies the bytes into a new c_longlong, using the byte order of the machine:
For data with a fixed byte order, such as a network protocol or a file format, prefer int.from_bytes(raw, "big", signed=True) or the struct module with "<q" or ">q". c_longlong always uses the byte order of the CPU, so it is the right tool for in-memory structures shared with C, not for file formats.
On all mainstream platforms, yes. The C standard only guarantees at least 64 bits, but Windows, Linux and macOS use exactly 64. You can confirm with ctypes.sizeof(ctypes.c_longlong).
c_long is 4 bytes on Windows and 8 bytes on 64-bit Linux and macOS. c_longlong is 8 bytes everywhere, so it is the safer choice for a value that must be 64 bits.
Use c_int64 when the C declaration says int64_t, because it states the intent. They have the same size on supported platforms.
Use ctypes.c_ulonglong or ctypes.c_uint64. The range is 0 to 18,446,744,073,709,551,615.
ctypes.c_longlong is the signed 64-bit integer type for calls into C. It wraps on overflow, converts back to a plain Python int, and works in arrays and structures. Always declare argtypes and restype, check the range of values before passing them in, and use struct or int.from_bytes when the byte order is fixed by a file or protocol.