Using C in Python was never this Easy

Ayushman Kumar
3 min readApr 10, 2022

Use your C functions in Python or any other Programming Langauge.

You might be thinking, there are a lot of blogs on the internet on how to use C in Python. This is gonna be different, just follow along 😎.

We are going to utilize the .dll format to communicate between C and Python. DLL stands for dynamic link library. If you were ever into gaming, I bet you have come across this format at some point in your life.

“ERROR: The application cannot start because the XYZ.dll file was missing. Re-Installing the app…”

Haha! Now let’s see what and how are we going to proceed with the blog.

  1. Write a Script in C and Execute
  2. Install Scons in Python
  3. Make the C code compatible with export functions to the DLL file.
  4. Run the functions with Python using our DLL file.

Let’s Write a simple Script to add two numbers in C

We will be using two files — “example.c” and “example.h”. We will write two functions:

  1. Find if the number is a Prime or Composite number
  2. Find if the number is odd or even

Now let’s dive into the code:

example. h

#ifndef EXAMPLE_H#define EXAMPLE_H
#include <stdio.h>
int prime(int n);int odd_even(int n);
#endif

example.c

#include "example.h"int prime(int n) {  int i;  for (i = 2; i < n; i++) {    if (n % i == 0) {      return 0;    }
}
return 1;}
int odd_even(int n) { if (n % 2 == 0) { return 0; } return 1;}

Install Scons using PIP

We are going to use SConstruct for building our C program.

pip install scons

Create a new file — “SConstruct” in the same directory and write

SharedLibrary("output", "example.c")

Save the file and Go to Terminal and write:

scons
Output of Scons

This create a DLL file — “output.dll” but we cannot use the C functions yet.

Make C compatible with DLL Export

Now, let’s make the C functions useable from dll file. We need to add “__declspec(dllexport)” before function declaration in example.h.

example.h

#ifndef EXAMPLE_H#define EXAMPLE_H
#include <stdio.h>__declspec(dllexport) int prime(int n);__declspec(dllexport) int odd_even(int n);
#endif

Again go to the terminal and type “scons”. This will now build a new “output.dll”. Now let’s test our C functions in Python.

Run DLL files on Python

We create a new “test.py” in the same directory.

test.py

import ctypes
dll = ctypes.CDLL("./output.dll")
print("Output of Prime(9):", dll.prime(9))print("Output of Prime(13):", dll.prime(13))print("Output of odd_even(17):", dll.odd_even(17))print("Output of odd_even(8):", dll.odd_even(8))

Output:

Using our C functions in Python

Hoorayyyy! We are successful in our mission!

Why should we use DLL files?

  1. It’s an independent file. You can paste the file anywhere and it will be usable.
  2. The code behind the DLL file remains hidden from the end user.
  3. It’s independent of any major Python code modification.
  4. DLL Files are very light weight
  5. DLL is one of the standard files on Windows.

I hope you like this Blog.

Github Link — https://github.com/ayushmankumar7/C_in_Python

Keep hustling! Keep exploring! Together we can make it happen!

--

--