• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About Us
    • About CSEStack:
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • Campus Ambassador
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • C/C++
    • Python
    • Java
    • HTML CSS
    • SQL
  • CSE Subject
    • Compiler Design
    • Computer Network
    • COA
    • Data Structure
    • DBMS
    • Operating System
    • Theory of Automata
    • Web Technology
  • Linux
  • Trend
    • Artificial Intelligence (AI)
    • Big Data
    • Cloud Computing
    • Machine Learning (ML)
  • GATE CSE 2020
    • Self Study Plan
    • Complete Syllabus
    • FREE Test Series
    • Topper Interview (AIR 15)
    • Recommended Books by Topper
  • Career
    • Placement Interview
    • Jobs
    • Aptitude
    • Quiz
  • Material
    • Recommended Books
    • Software Installation
  • Contribute to Us
    • Write for Us
    • Submit Source Code or Program
    • Share Interview Experience
  • Tools

Calling C Functions from Python [Step by Step Procedure]

Aniruddha Chaudhari/05 Aug, 17/9225/6
C / C++CodePython

Here, in this tutorial, I am going to tutor you a simple Python wrapper for C library. And how to call C functions from Python?

Before beginning this, let’s see…

Why do we need of Calling C Functions from Python?

Till now I have talked about many plus points about Python.

But…

There is One of the major disadvantages of the Python – its execution speed. 🙁  Python is slower than many of the competitive programming language like C, Java…

C and CPP is a very standard programming language. And there are so many libraries already available in C.

To enhance the speed of the Python and to make the reuse of existing C libraries, the concept of calling C functions from Python is useful.

So there are two advantages of using C libraries (or C functions):

  • As the library is compiled from C programming, it increases the spread of execution. (C program runs faster than Python program.)
  • There are many C libraries are already build. Reusing existing C libraries saves time and resource.

In fact, there are many Python inbuilt standard libraries are written in C. You may not have noticed it, but many times when you call inbuilt function, it executes C function internally.

So coming to our coding part…

How to create these C libraries?

In this tutorial, I am going to tell you, how to compile C program to create library and step by step procedure for calling C functions from Python code.

Creating C library and Calling C Functions from Python:

Steps…

  1. Write a C program with function definition
  2. Write a C program header file with all function declaration
  3. Create C library that can be used in Python
  4. Calling C functions from Python program

To understand this tutorial, we will create an arithmetic application where all the arithmetic definition are written in C. And we will write a wrapper using Python called calcithat will call the function written in C program.

Let’s begin…

Step 1: Write a C program with functions definition

First of all, write a simple C program with functions definition.

Even if you know the basic of C and C++ programming, you can write the function in C that performs the basic arithmetic operations.

Following is code in C.

#include <stdio.h>
#include <stdlib.h>
#include "arithmatic.h"

void connect()
{
	printf("Connected to C extension...\n");
}

//return random value in range of 0-50
int randNum()
{
        int nRand = rand() % 50; 
	return nRand;
}

//add two number and return value
int addNum(int a, int b)
{
	int nAdd = a + b;
	return nAdd;
}

Note: There is no function main() in above program. There are only function definitions that we are going to call them from Python program.

Save above program as arithmatic.c.

Step 2: Write a C Program Header File with all Function Declaration

We may have noticed in above C program as we have included header file as arithmatic.h.

void connect();
int randNum();
int addNum(int a, int b);

This header file contains declarations of all the arithmetic functions.

Save the file as aithmatic.h. in the same directory.

Step 3: Create C library that can be used in Python

You need gcc compiler to compile C program.

To use that C function in Python you need to compile and generate the shared object.

Now Compile C program Using gcc Compiler:

gcc -c -fPIC arithmatic.c -o arithmatic.o

It will generate .o file. Run below command to get shared object .so file from .o.

gcc hello.o -shared -o libcalci.so

It will create the file libcalci.so.

Another way using the Single Command:

We are only interested in file .so (shared object). So, we can skip creating .o file.

You can merge above two commands into one, that will give you shared object without getting .o object file.

gcc -shared -o libcalci.so -fPIC arithmatic.c

After running this command you see th file libcalci.so is generated in current directory.

Backend C library (libcalci.so) is ready to use. We can import it into our Python program to use it.

Note: You need to generate shared object .so file for Linux, OS X, etc. For the Window OS,  you need to generate .dll file.

Step 4: Calling C Functions from Python Program

As like Python programming, writing wrapper is easier than it sounds.

To call C functions from Python wrapper, you need to import ctypes foreign function library in Python code.

(You can read more about this library on Python official document.)

Load .so file that we have created in the previous step by creating an instance of CDLL. Just call to its constructor function CDLL(). It returns object variable (says libCalc).

Now, with this object variable, you can call and pass input parameters to C functions just like calling normal Python class functions.

Here is simple code…

from ctypes import *
libCalc = CDLL("./libcalci.so")

#call C function to check connection
libCalc.connect() 

#calling randNum() C function
#it returns random number
varRand = libCalc.randNum()
print "Random Number:", varRand, type(varRand)

#calling addNum() C function
#it returns addition of two numbers
varAdd = libCalc.addNum(20,30)
print "Addition : ", varAdd

Output:

Connected to C extension...
Random Number: 27 <type 'int'>
Addition: 50

In the program, we used function type() to identify return data type of the C function’s variable.

This is all about creating C library and calling C functions with Python. If you have any question or doubt, write in a comment. I do reply all comments.

Some points to Acknowledge:

  • We are using ctype native Python module to load shared library.
  • The ctype Python module is available from 2.5 Python version. So, just ensure you have the latest version installed on your system
  • All these commands and programs in this tutorial are run on Ubuntu Linux system. And it will work for all other systems as well.

Before learning Python, I learned C programming. Whenever I require, many times I reuse existing C programs (those are written earlier on my system) in Python. It saves my lots of time. Even, it is the useful approach of writing your own C library and executing complex logic faster in C functions.

Hope you enjoy this tutorial.

If you want to be the expert Python programmer, you can follow my complete Python tutorial for FREE.

Happy Programming!

cppctypeProgrammingPython

Related Posts

Jobs

Top 5 Computer Programmer Skills You Require to Land Better Jobs

C / C++Code

Nested Switch Statements in C Programming with Real Life Example

C / C++CodeJAVAPython

[Explained in Detail] if else nested Programming Example in C, Java and Python

Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack Portal.

Comments

  • Reply
    Akshay
    June 20, 2018 at 6:39 pm

    How to access global variable of c in python ?

    • Reply
      Aniruddha Chaudhari
      June 24, 2018 at 11:22 pm

      Hi Akshay,

      I did not try it. Will try it and will let you know.

  • Reply
    Perry
    June 24, 2018 at 10:56 pm

    Can I call C program from python that uses wiringPi to write to an LCD 20×2 through an MCP23017 chip? I am using the LCD 20×2 to display the date and time and CPU temperature of a raspberry pi.

    Thank you

    • Reply
      Aniruddha Chaudhari
      June 24, 2018 at 11:20 pm

      Hi Perry.

      I am not much into raspberry pi and other hardware chips, and did not come across this situation. So not sure right now.

      Thanks!

  • Reply
    Jinnu
    October 11, 2018 at 6:07 pm

    Hi,

    Is it possible to call Scala code from python without pyspark like above code (calling c code from python)? Is it possible to call Scala code from python like above?

    • Reply
      Aniruddha Chaudhari
      October 11, 2018 at 7:36 pm

      Hi Jinnu,

      I am not sure about it as I have never tried that.

Leave a Reply Cancel reply

Interview Experiences

AccentureAgriChain Akamai Amazon Amdocs American Express Attra Infotech BARC Barclays BlockGrain BYJUS Cisco Cognizant Coupon Dunia Credit Suisse DE Shaw Druva Experis IT Eze Software Factset Fiorano General Electric Incture Technologies Infosys Kasmo Cloud Microsoft MindTree Mu Sigma Numerify Opteamix Oracle Persistent Pole to Win Qualcomm Reliance Riverbed Syntel TCS Tech Mahindra Teradata Terralogic Virtusa Wipro

Interview Questions



You can share your interview experience.

Don’t Miss !

Latest Articles

Cisco Online Test Pattern and Interview Questions for Freshers


[7 Best Tips] How to Make Daily Study Timetable and Stick to It?


Persistent Written Test and Technical Coding Questions [Paper Pattern]


10 Top Website Design Tips for Small Business to Boost Up Your Sales


Main Difference Between remove del and pop in Python List


Tech Mahindra Placement (Online Test | Essay Writing | Interview Questions)


Importance of Career Exploration for High-School Students | Why?


Favorite Topic

AI algorithm array bigdata bit manipulation career Code Computer Network cpp data analytics database data scientist Data Structure db DBMS difference between Django education GATE GATE Topper Interview HTML ibm IBM ISDL interview IP address Java JavaScript Jobs Linked List linux linux cmd nit OOPs Concept os Programming Python python list Qualcomm SOAP sorting stack string vim webbrowser web development

Contribution to Community

  • Contribute to CSEstack Portal
  • Submit Your Source Code or Program
  • Share Your Interview Experience
  • CSEStack Leadership Board
  • Campus Ambassador Program by CSEstack

About CSEstack Portal

  • About CSEStack:
  • Contact Us
  • CSEStack Campus Ambassador
  • Recommended Books by Expert

© 2019 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About Us
    • About CSEStack:
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • Campus Ambassador
  • LeaderBoard