Python Glossary Русский

all Python 3ctypesterm

ctypes.c_int

C types

The C int type in ctypes: a mutable wrapper around a fixed-width number for passing to C functions.

Syntax

x = ctypes.c_int(42)
x.value  # → 42

Unlike Python's int the width is fixed, and extra bits are silently dropped: c_int(2**32 + 1).value is 1. The value is read and changed via the .value attribute. Alongside sit c_double, c_char_p, c_size_t and other types — ctypes.sizeof gives their size. Pass Python strings as bytes or via c_char_p, not as str.

Examples

import ctypes
x = ctypes.c_int(42)
print(x.value)   # → 42
x.value += 1
print(x.value)   # → 43
print(ctypes.c_int(2**32 + 1).value)   # → 1 — старшие биты отброшены
print(ctypes.sizeof(ctypes.c_double))   # → ? обычно 8 байт

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy