Python Academy · Lesson

ctypes: Calling C Libraries from Python

Load shared libraries and call C functions using ctypes.

Lesson 2 of 413 steps

ctypes: Calling C Libraries from Python is a free Python Academy lesson on CoddyKit. This is lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, and your progress syncs across the web and the CoddyKit app. The Python Academy course includes 4 lessons in total.

What Is ctypes?

ctypes is a standard-library module for loading shared C libraries and calling their functions from Python — no compilation required.

import ctypes

# Load the C standard library
libc = ctypes.CDLL("libc.so.6")   # Linux
# libc = ctypes.CDLL("libSystem.dylib")  # macOS

libc.puts(b"Hello from C!")

Loading Libraries

Use ctypes.CDLL for cdecl calling convention (most C libraries) and WinDLL for Windows stdcall APIs.

import ctypes

# Platform-independent approach:
import ctypes.util
libm_name = ctypes.util.find_library("m")
libm = ctypes.CDLL(libm_name)

result = libm.sqrt
result.restype = ctypes.c_double
result.argtypes = [ctypes.c_double]
print(result(2.0))   # 1.4142...

C Data Types

Map Python types to C types using ctypes classes: c_int, c_double, c_char_p, c_void_p, etc.

import ctypes

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

d = ctypes.c_double(3.14)
print(d.value)   # 3.14

s = ctypes.c_char_p(b"hello")
print(s.value)   # b"hello"

Setting argtypes and restype

Always declare argtypes (parameter types) and restype (return type) to avoid silent crashes from type mismatches.

import ctypes, ctypes.util

libm = ctypes.CDLL(ctypes.util.find_library("m"))

libm.pow.argtypes = [ctypes.c_double, ctypes.c_double]
libm.pow.restype  = ctypes.c_double

print(libm.pow(2.0, 10.0))   # 1024.0

Passing Pointers

Use ctypes.byref(var) or ctypes.pointer(var) to pass a pointer to a C variable.

import ctypes

libc = ctypes.CDLL(None)   # None = current process

value = ctypes.c_int(0)
# Passing pointer to C function:
# libc.some_func(ctypes.byref(value))
print(value.value)

Structs

Define C structs by subclassing ctypes.Structure and listing _fields_.

import ctypes

class Point(ctypes.Structure):
    _fields_ = [
        ("x", ctypes.c_double),
        ("y", ctypes.c_double),
    ]

p = Point(1.0, 2.0)
print(p.x, p.y)   # 1.0 2.0

Arrays

Create C arrays with TypeClass * N.

import ctypes

IntArray5 = ctypes.c_int * 5
arr = IntArray5(10, 20, 30, 40, 50)
for v in arr:
    print(v, end=" ")   # 10 20 30 40 50

Callbacks (Function Pointers)

Use ctypes.CFUNCTYPE to create a C-callable Python function to pass as a callback.

import ctypes

CompareFunc = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int, ctypes.c_int)

def my_compare(a, b):
    return a - b

c_compare = CompareFunc(my_compare)
# Pass c_compare to a C function expecting a comparator

Loading Windows DLLs

On Windows, use ctypes.WinDLL for DLLs using stdcall convention (e.g., the Windows API).

import ctypes   # Windows only

kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
kernel32.GetTickCount.restype = ctypes.c_ulong
print(kernel32.GetTickCount())

Error Handling

Check ctypes.get_last_error() (Windows) or use Python-level try/except for errors returned as special values.

import ctypes

# Many C functions return -1 or NULL on error:
result = some_c_function()
if result == -1:
    raise OSError("C function failed")

# For errno on POSIX:
import os
if result < 0:
    raise OSError(os.strerror(ctypes.get_errno()))

Practical Example: SHA-256 with OpenSSL

Call OpenSSL's SHA256 directly via ctypes without any Python binding library.

import ctypes, ctypes.util

libcrypto = ctypes.CDLL(ctypes.util.find_library("crypto"))

SHA256 = libcrypto.SHA256
SHA256.argtypes = [ctypes.c_char_p, ctypes.c_size_t, ctypes.c_char_p]
SHA256.restype  = ctypes.c_char_p

buf = ctypes.create_string_buffer(32)
SHA256(b"hello", 5, buf)
print(buf.raw.hex())

Quick Check

Why should you always set argtypes and restype on a ctypes function?

Recap

ctypes loads shared C libraries without compilation. Declare argtypes and restype on every function. Use Structure for C structs, TypeClass * N for arrays, and CFUNCTYPE for callbacks.

Free to start

Learn Python with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
76
Lessons
320

Frequently Asked Questions

Is the “ctypes: Calling C Libraries from Python” lesson free?

Yes — the full text of “ctypes: Calling C Libraries from Python” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO. The Python Academy course includes 4 lessons in total.

What will I learn in “ctypes: Calling C Libraries from Python”?

Load shared libraries and call C functions using ctypes. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “ctypes: Calling C Libraries from Python” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why C Extensions? Use Cases and Trade-offs
  2. ctypes: Calling C Libraries from Python
  3. cffi: C Foreign Function Interface
  4. Writing a Python C Extension Module
← Back to Python Academy