14.14.2.7 Fundamental data types

class _SimpleCData
This non-public class is the base class of all fundamental ctypes data types. It is mentioned here because it contains the common attributes of the fundamental ctypes data types. _SimpleCData is a subclass of _CData, so it inherits their methods and attributes.

Instances have a single attribute:

value
This attribute contains the actual value of the instance. For integer and pointer types, it is an integer, for character types, it is a single character string, for character pointer types it is a Python string or unicode string.

When the value attribute is retrieved from a ctypes instance, usually a new object is returned each time. ctypes does not implement original object return, always a new object is constructed. The same is true for all other ctypes object instances.

Fundamental data types, when returned as foreign function call results, or, for example, by retrieving structure field members or array items, are transparently converted to native Python types. In other words, if a foreign function has a restype of c_char_p, you will always receive a Python string, not a c_char_p instance.

Subclasses of fundamental data types do not inherit this behaviour. So, if a foreign functions restype is a subclass of c_void_p, you will receive an instance of this subclass from the function call. Of course, you can get the value of the pointer by accessing the value attribute.

These are the fundamental ctypes data types:

class c_byte
Represents the C signed char datatype, and interprets the value as small integer. The constructor accepts an optional integer initializer; no overflow checking is done.

class c_char
Represents the C char datatype, and interprets the value as a single character. The constructor accepts an optional string initializer, the length of the string must be exactly one character.

class c_char_p
Represents the C char * datatype, which must be a pointer to a zero-terminated string. The constructor accepts an integer address, or a string.

class c_double
Represents the C double datatype. The constructor accepts an optional float initializer.

class c_float
Represents the C float datatype. The constructor accepts an optional float initializer.

class c_int
Represents the C signed int datatype. The constructor accepts an optional integer initializer; no overflow checking is done. On platforms where sizeof(int) == sizeof(long) it is an alias to c_long.

class c_int8
Represents the C 8-bit signed int datatype. Usually an alias for c_byte.

class c_int16
Represents the C 16-bit signed int datatype. Usually an alias for c_short.

class c_int32
Represents the C 32-bit signed int datatype. Usually an alias for c_int.

class c_int64
Represents the C 64-bit signed int datatype. Usually an alias for c_longlong.

class c_long
Represents the C signed long datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

class c_longlong
Represents the C signed long long datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

class c_short
Represents the C signed short datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

class c_size_t
Represents the C size_t datatype.

class c_ubyte
Represents the C unsigned char datatype, it interprets the value as small integer. The constructor accepts an optional integer initializer; no overflow checking is done.

class c_uint
Represents the C unsigned int datatype. The constructor accepts an optional integer initializer; no overflow checking is done. On platforms where sizeof(int) == sizeof(long) it is an alias for c_ulong.

class c_uint8
Represents the C 8-bit unsigned int datatype. Usually an alias for c_ubyte.

class c_uint16
Represents the C 16-bit unsigned int datatype. Usually an alias for c_ushort.

class c_uint32
Represents the C 32-bit unsigned int datatype. Usually an alias for c_uint.

class c_uint64
Represents the C 64-bit unsigned int datatype. Usually an alias for c_ulonglong.

class c_ulong
Represents the C unsigned long datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

class c_ulonglong
Represents the C unsigned long long datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

class c_ushort
Represents the C unsigned short datatype. The constructor accepts an optional integer initializer; no overflow checking is done.

class c_void_p
Represents the C void * type. The value is represented as integer. The constructor accepts an optional integer initializer.

class c_wchar
Represents the C wchar_t datatype, and interprets the value as a single character unicode string. The constructor accepts an optional string initializer, the length of the string must be exactly one character.

class c_wchar_p
Represents the C wchar_t * datatype, which must be a pointer to a zero-terminated wide character string. The constructor accepts an integer address, or a string.

class HRESULT
Windows only: Represents a HRESULT value, which contains success or error information for a function or method call.

class py_object
Represents the C PyObject * datatype. Calling this without an argument creates a NULL PyObject * pointer.

The ctypes.wintypes module provides quite some other Windows specific data types, for example HWND, WPARAM, or DWORD. Some useful structures like MSG or RECT are also defined.

See About this document... for information on suggesting changes.