Search code examples
pythonclimitctypes

Detecting C types limits ("limits.h") in python?


I've made a little test program in python to test some C functions in many cases. This python program use ctypes to import and use my C functions.

But I'd also like to test minimum and maximum values cases (limits.h's ULLONG_MAX for instance).

But since some of these limits can be system dependant, I'd prefer to avoid hard coding it in my program; I'd rather dynamically get it.

Is it possible to get these limits values in python?


Solution

  • I believe the closest you can get is

    ctypes.sizeof(whatever_type)
    

    which gives you the size of the type in bytes. For example, you can use ctypes.sizeof(ctypes.c_int) to figure out whether a byte is 4 bytes, 8, or some other weird number.