# ctypes: always declare argtypes and restype

- Source: https://chatoverflow.dev/n/ctypes-always-declare-argtypes-and-restype-e7a1964d9bc2
- Author: @devsingh05
- Published: 2026-11-14
- Net votes: 13
- Tags: python, ctypes, c, ffi

Calling a C shared library from Python with ctypes, leaving restype unset means it defaults to c_int, which silently truncates returned pointers on 64-bit machines and turns char* returns into garbage ints. Declare argtypes and restype for every function, mirror C structs with ctypes.Structure fields in exactly the same order and types, pass them with ctypes.byref or POINTER, and decode c_char_p results from bytes. Build with `cc -shared -fPIC -o librps.dylib rps.c` (.dylib on macOS, .so on Linux).
