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

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Calling C Functions from Python [Step by Step Procedure]

Aniruddha Chaudhari/83781/44
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 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 a C program to create a 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 a function definition
  2. Write a C program header file with all function declaration
  3. Create a 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 basics 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 the above program. There are only function definitions that we are going to call them from the Python program.

Save the 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 a 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 the shared object .so file from .o.

gcc arithmatic.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 the file libcalci.so is generated in the 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 the 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 a 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 the 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 to all comments.

Some points to Acknowledge:

  • We are using ctype native Python module to load the 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 me 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!

cppctypePython
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a 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.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

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
        Ewig
        August 14, 2020 at 4:24 pm

        Hey Aniruddha,

        Have you ever tried writing a python wrapper to create a C file and add instructions into the C file using python scripting? and later compile it in the end?

        • Reply
          Aniruddha Chaudhari
          August 15, 2020 at 9:53 pm

          Hi Ewig,

          I did not come across any such necessity. But, I think it’s doable.

        • Reply
          Bdub
          December 16, 2020 at 5:30 am

          This is definitely doable and as simple as writing to files like any other in Python just with a .c extension and then using a shell call to compile said files.

  • 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
      Hadi
      February 28, 2020 at 5:44 pm

      Many have done similar projects for Micropython, a python distribution for microcontrollers. Just google for it. surprisingly, these projects have made use of Python for raspberry pi and Arduino projects source code.

      This article really helped me understand C. Thanks, Aniruddha Chaudhari.

      • Reply
        Aniruddha Chaudhari
        February 28, 2020 at 6:56 pm

        Thanks, Hadi for sharing your thought. I hope, Perry will find it helpful.

  • 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.

  • Reply
    Nishit Sethi
    June 11, 2019 at 3:03 am

    Hey, how can I use a C function to create a new column in a pandas data frame using “apply”?

    • Reply
      Aniruddha Chaudhari
      June 11, 2019 at 7:51 am

      C functions are used to implement some algorithms and logic. It is difficult to retain the Python data types in C.

      In your case, you can write C function that manipulates and creates the desired array. Return this array data to the Python and then append that array to Pandas data frame as a new column in Python.

  • Reply
    an
    July 29, 2019 at 2:59 pm

    How can I do it in the opposite way? like How can I call python functions from C programming?

    • Reply
      Aniruddha Chaudhari
      July 30, 2019 at 10:42 am

      You can call Python function from C. It is not so straightforward though. You can refer to this document.

  • Reply
    Brian Concannon
    August 15, 2019 at 9:47 pm

    Your page /calling-c-functions-from-python was the only internet source that worked right for me.

    BTW change gcc hello.o to gcc arithmetic.o, otherwise it fails.

    • Reply
      Aniruddha Chaudhari
      August 15, 2019 at 10:00 pm

      Hi Brian, I am glad as this page has helped you to solve your problem.

      Thanks for the correction. Edited!

  • Reply
    Cassio
    October 15, 2019 at 8:05 pm

    Hello!
    I have a problem with the C function parameters.
    When I set a string parameter in a C function through python, it reads only the first character of the string. I’m wondering if it’s a type error because the string type in python is different from C.
    how can I fix this? Is there a way to pass a string that is compatible with C?

    note: this problem occurs only when I use python3.7, if I use python2.7 interpreter, it works fine.

    note2: sorry about my English. I’m Brazilian xD

    • Reply
      Aniruddha Chaudhari
      October 16, 2019 at 9:05 am

      Hi Cassio,

      As your code is working with Python2.7 and not working with Python 3.7, there is a change in string implementation in both the versions.

      By default string in Python 2 is ASCII type whereas string in Python 3 is Unicode. Try typecasting the string in your code.

      Please let me know if this works for you.

  • Reply
    Maniganda
    October 25, 2019 at 11:32 am

    How to access the char array returned from C program, as of now the program is returning the memory address of the pointer.

    • Reply
      Aniruddha Chaudhari
      October 30, 2019 at 2:06 pm

      Maniganda, if you know the address of pointer you can access the pointer and then character string. Not sure about the problem you are facing.

  • Reply
    Manomugdha Biswas
    December 27, 2019 at 9:00 am

    Is it possible to call c functions of Linux c executable (not .so file) from python?

    • Reply
      Aniruddha Chaudhari
      December 27, 2019 at 9:03 am

      You can not do it directly. You can execute a compiled C program (.exe file) from the Python program using a subprocess module by running the executable file using subprocess.open() method in Linux.

  • Reply
    Shaleen Jain
    January 23, 2020 at 4:03 pm

    Hi,
    How can I do so if I have string arguments and double Array arguments?

    • Reply
      Aniruddha Chaudhari
      January 24, 2020 at 10:09 am

      Hi Shaleen.

      This will be tricky. Whenever we return a string or double array, the function does not return actual value, but it returns a pointer to these data objects.

      You have to explicitly save these arguments in the dynamic memory and then return the reference from C to Python.

  • Reply
    pooja chakraborty
    January 23, 2020 at 5:22 pm

    Hi,

    Actually I am using dalsa teledyne camera which has a default .so called libGevapi.so file. I am trying to access the functions of that library from python using ctypes method as shown by you. The functions can be accessed but it is not returning correct results.
    Jst, for example, there is a function called GevDevicecount which returns no. of devices connected to the system. When I am directly running the C code to access the function it returns 2 devices connected. But when I access the same function from python using ctypes method it returns 0. where 2 is the correct result.

    can you help me where the issue is?

    • Reply
      Aniruddha Chaudhari
      January 24, 2020 at 10:03 am

      Hi Pooja. Looks like there is a mismatch of the return data type. Try with different expected return values (says 3, 4), if it is still returning 0, most probably a problem is with the expected data type of the return variable.

  • Reply
    Nick Harrington
    January 31, 2020 at 4:38 am

    Hey, very helpful tutorial, thanks so much! My question is, how do I use Python to run a C function that normally needs to be run with sudo?

    • Reply
      Aniruddha Chaudhari
      February 1, 2020 at 7:28 pm

      If the user running the Python program has root privilege, you can run the C function (even it needs to be run with Sudo). Another way, you can grant the executable permission to the C program for the normal user.

  • Reply
    Mathias
    April 9, 2020 at 11:25 am

    This step by step tutorial is a neat solution to exactly the problem I was facing. Very concise and clear. Thank you! Kind regards, Mathias from Germany.

    • Reply
      Aniruddha Chaudhari
      April 9, 2020 at 12:09 pm

      Wow. You’re welcome, Mathias! I hope you will enjoy going through my other tutorials also. And Kindly share your feedback about other tutorials if you go through any. Stay safe!

  • Reply
    Peter Galan
    April 11, 2020 at 3:34 am

    Excellent tutorial, congratulation! I am just looking for a (and can’t find any) suitable makefile, in which I would just list all .o files (created from their c source files) and finally would get their .so files using the same names as the original C source files. Do you have some ideas? Thanks in advance for your reply.

    Regards,
    Peter

    • Reply
      Aniruddha Chaudhari
      April 11, 2020 at 6:04 pm

      Thanks Peter for your kind words!

      Can you check the make file tutorial? And please let me know if you still have a problem.

  • Reply
    Peter Galan
    April 14, 2020 at 2:07 am

    Thanks for a prompt reply. Well, I have to admit that I am a little bit “rusty” with the makefiles as I haven’t used them for the last 15 years (a side efect of using MS Visual Studio 🙂 ), but yes, there is no problem to write and execute something like:

    LibFun1 : Fun1.c Fun1.h
        gcc -o LibFun1 -shared Fun1.c
    

    But imagine, I want to have several other pairs of Funx.c and Funx.h in the same folder and to create the shared library files of all of them. If I write the makefile like:

    LibFun1 : Fun1.c Fun1.h
        gcc -o LibFun1 -shared Fun1.c
    
    LibFun2 : Fun2.c Fun2.h
        gcc -o LibFun2 -shared Fun2.c
    

    only the first command line (for LibFun1) will be executed. So, is there a way to create a makefile such a way that all shared library files will be created with just one “make” call?

    And I have a second question regarding a shared library function, which would return more than just a single value. For example, in your tutorial the Connect() function will print the text into the C “console”, but I want to see it in the Python “window”. So, how can the Connect() function return an entire string to the Python variable?

    Thanks in advance for your reply!

    Regards,
    Peter

  • Reply
    Peter Galan
    April 14, 2020 at 8:55 pm

    Hi Aniruddha,
    I made further experiments (well, on the Raspberry Pi, where I am doing all these experiments), and I found out that to return a string (or any aggregated variable) from a C function to Python program, you can do so only via a reference type function argument. Return won’t work! For example, the code which normally work in a C program:

    char st[] = "Hello";
    
    char *RetStr(void)
    {
         // char st[] = "Hello";
         return st;
    }
    
    void main()
    {
         printf("Remote greeting: %s", RetStr());
    }
    

    will not work with the Python. Instead of the string, the RetStr() function will return a number, apparently the st address. The only way (well, based on my limited observations) how to return a string is as follows:

    char st[] = "Hello";
    
    void RetStr(char s[])
    {
         strcpy(s, st);
    }
    

    and in the Python program to call this C function as:

    txt = ""                "
    
    libClib.RetStr(txt)
    

    And the txt has to be long enough to receive the entire text string from C. If txt is defined like txt = "", nothing will be copied. This surprises me, as I expected that the RetStr() function would create another txt object with the length and content depending only on what RetStr() provides.

    Regards,
    Peter

    • Reply
      Aniruddha Chaudhari
      April 14, 2020 at 10:42 pm

      Thanks, Peter for sharing your experience. This also surprises me a bit. It’s been a while I worked on this. I have to look into it. I will share if I get any different thoughts.

  • Reply
    Peter Galan
    April 15, 2020 at 9:32 pm

    Hi Aniruddha,
    As sitting at home due to both, pandemic and bad weather (imagine, we’ve got snow again in Ottawa, Canada) I have a lot of time to play with the Raspberry Pi, so I found out how to properly retrieve a string value returned by a C function as a pointer to char.
    Here is the C library function:

    char st[] = "Connection to library.";
    
    char *Connect(void)
    {
         return st;
    }
    

    And here is the Python code:

    from ctypes import *
    libCalc = CDLL("./libcalci.so")
     
    #call C function to check connection
    libCalc.Connect.restype = c_char_p    # instruct Python that given function returns char*
    print libCalc.Connect() 
    

    Regards,
    Peter

    • Reply
      Aniruddha Chaudhari
      April 16, 2020 at 11:09 am

      Hi Peter,

      Yeah, time is not really good.I’m really impressed the way you are utilizing this time at the best.

      At last, you featured it out the passing string. Awesome!

      Stay Home and Stay Safe, Peter!

      And keep sharing your findings with us.

      Regards,
      Ani

  • Reply
    Jeff
    May 21, 2020 at 2:12 am

    You are a star, sir! A helpful tutorial.

    • Reply
      Aniruddha Chaudhari
      May 23, 2020 at 4:30 am

      Thanks, Jeff! It’s always a pleasure to see if the readers finding tutorial helpful for them.

  • Reply
    Woody Pope
    August 23, 2020 at 11:15 am

    I cannot get my C code to return a string. Is there a secret to it?
    Is there something special to be done on the python 3 side?

    • Reply
      Aniruddha Chaudhari
      August 24, 2020 at 8:11 am

      Not sure what exactly is the problem. Just to give you heads up, in Python 2, string type is ASCII. And in Python 3, string type is Unicode. Check if you have a mismatch with the string formating.

  • Reply
    Debnandini
    October 18, 2021 at 1:31 am

    suppose I have a c code that compiles using external libraries (along with paths), in the following way:

    clang -Xpreprocessor -I/opt/local/include/libomp -L/opt/local/lib/libomp -lomp -w -o SpecFit SpecFit.c -lgsl  -lm

    then in your step 3, should I be adding these extra arguments when making the shared object (as below)?

    gcc -shared -o SpecFit.so -fPIC SpecFit.c -Xpreprocessor -I/opt/local/include/libomp -L/opt/local/lib/libomp -lomp -w -lgsl  -lm
    • Reply
      Aniruddha Chaudhari
      January 29, 2022 at 11:19 am

      I’m really not sure about those extra attributes you mentioned. Still, I would recommend trying with and without these attributes. Whichever works, it would be the way to go.

Leave a Reply Cancel reply

Basic Python Tutorial

  1. Python- Tutorial Overview
  2. Python- Applications
  3. Python- Setup on Linux
  4. Python- Setup on Windows
  5. Python- Basic Syntax
  6. Python- Variable Declaration
  7. Python- Numeric Data Types
  8. Python- NoneType
  9. Python- if-else/elif
  10. Python- for/while else
  11. Python- User Input
  12. Python- Multiline User Input
  13. Python- String Formatting
  14. Python- Find Substring in String
  15. Python- Bitwise Operators
  16. Python- Range Function
  17. Python- List
  18. Python- List Vs Tuple
  19. Python- Compare Two Lists
  20. Python- Sorting List
  21. Python- Delete Element from List
  22. Python- Dictionary
  23. Python- ‘is’ vs ‘==’
  24. Python- Mutable vs Immutable
  25. Python- Generator & Yield
  26. Python- Fibonacci Generator
  27. Python- Assert Statement
  28. Python- Exception Handling 
  29. Python- RegEx
  30. Python- Lambda Function
  31. Python- Installing Modules
  32. Python- Important Modules
  33. Python- Find all Installed Modules
  34. PyCharm- IDE setup
  35. Python- File Handling
  36. Python- Monkey Patching
  37. Python- Decorators
  38. Python- Instance vs Static vs Class Method
  39. Python- Name Mangling
  40. Python- Working with GUI
  41. Python- Read Data from Web URL
  42. Python- Memory Management
  43. Python- Virtual Environment
  44. Python- Calling C Function

Python Exercise

  1. Python- Tricky Questions
  2. Python- Interview Questions (60+)
  3. Python- Project Ideas (45+)
  4. Python- MCQ Test Online
  5. Python- Coding Questions (50+)
  6. Python- Competitive Coding Questions (20+)

Python String

  1. Reverse the String
  2. Permutations of String
  3. Padding Zeros to String/Number

Python List

  1. Randomly Select Item from List
  2. Find Unique Elements from List
  3. Are all Elements in List Same?

Python Dictionary

  1. Set Default Value in Dictionary
  2. Remove all 0 from a dictionary

File Handling

  1. Python- Read CSV File into List
  2. Check if the File Exist in Python
  3. Find Longest Line from File

Compilation & Byte Code

  1. Multiple Py Versions on System
  2. Convert .py file .pyc file
  3. Disassemble Python Bytecode

Algorithms

  1. Sorting- Selection Sort
  2. Sorting- Quick Sort

Other Python Articles

  1. Clear Py Interpreter Console
  2. Can I build Mobile App in Python?
  3. Extract all the Emails from File
  4. Python Shell Scripting

© 2022 – CSEstack.org. All Rights Reserved.

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