restype is an attribute of a ctypes foreign function object that tells Python which C type the function returns. By default ctypes assumes every function returns a C int, so any other return type, such as double, char *, size_t, or a pointer, is misread unless you set restype. Setting it, together with argtypes, makes the call return the right Python value and prevents silent truncation and crashes.
When you call a function loaded with ctypes.CDLL or ctypes.WinDLL, ctypes reads the raw return register and converts it according to restype. The default is ctypes.c_int, which is 32 bits. That is why a function returning a 64-bit pointer or a double gives garbage or an exception with no setup.
restype value | C return type | Python result |
|---|---|---|
ctypes.c_int (default) | int | int |
ctypes.c_double | double | float |
ctypes.c_char_p | char * (NUL-terminated) | bytes or None |
ctypes.c_size_t | size_t | int |
ctypes.c_void_p | void * | int or None |
ctypes.POINTER(ctypes.c_int) | int * | pointer object |
None | void | None |
Create a small C library.
Compile it into a shared library.
Then call it from Python and set the types explicitly.
If you removed the restype line for hypotenuse, ctypes would read the result as an integer and return a meaningless number, because the double is returned in a floating-point register, not the integer one.
A C string return value needs c_char_p, and ctypes gives you bytes, which you decode yourself.
Be careful about ownership. If the C function allocates memory and returns it, c_char_p copies the bytes into a Python bytes object but does not free the original, so you must call the library's own free function. For that case use c_void_p so you keep the raw address:
For a function that returns a pointer to a structure, define the structure and use ctypes.POINTER(...).
A function returning a structure by value can use the structure class directly as restype. Whether it is safe depends on the platform calling convention, so test it on every target platform.
ctypes can also post-process the return value through errcheck, a function that receives the result, the function, and the arguments. It turns C error codes into Python exceptions.
restype as the default for pointer returns. On 64-bit systems the pointer is truncated to 32 bits and the program may crash later.argtypes. Without it, ctypes guesses argument types from the Python values, which breaks for double, large integers, and pointers.c_char_p for buffers that are not NUL-terminated. Use c_void_p and ctypes.string_at(addr, size).restype on the library instead of the function. It is a per-function attribute.CDLL for cdecl and WinDLL for stdcall on 32-bit Windows.ctypes.c_int. Any function whose real return type differs needs an explicit restype.
Set restype = None. The call then returns None.
You should set both. restype controls how the result is read and argtypes controls how arguments are passed and checked.
ctypes ships with Python and is fine for small bindings. For larger libraries, cffi can read C declarations from headers, which reduces type mistakes.
restype declares the C return type of a ctypes function so Python converts the result correctly. The default of c_int is wrong for doubles, 64-bit pointers, and strings. Set restype and argtypes for every function you call, use None for void, take care with memory ownership, and add errcheck to convert error codes into exceptions.