How to Call a C function in Python Last Updated : 01 Nov, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Have you ever came across the situation where you have to call C function using python? This article is going to help you on a very basic level and if you have not come across any situation like this, you enjoy knowing how it is possible.First, let's write one simple function using C and generate a shared library of the file. Let's say file name is function.c. C int myFunction(int num) { if (num == 0) // if number is 0, do not perform any operation. return 0; else // if number is power of 2, return 1 else return 0 return ((num & (num - 1)) == 0 ? 1 : 0) ; } Compile this : cc -fPIC -shared -o libfun.so function.cUsing ctypes(foreign function interface) library to call C function from PythonThe above statement will generate a shared library with the name libfun.so. Now, let's see how to make use of it in python. In python, we have one library called ctypes. Using this library we can use C function in python. Let's say file name is function.py. Python3 import ctypes NUM = 16 # libfun loaded to the python file # using fun.myFunction(), # C function can be accessed # but type of argument is the problem. fun = ctypes.CDLL("libfun.so") # Or full path to file # Now whenever argument # will be passed to the function # ctypes will check it. fun.myFunction.argtypes = [ctypes.c_int] # now we can call this # function using instant (fun) # returnValue is the value # return by function written in C # code returnVale = fun.myFunction(NUM) Comment More infoAdvertise with us Next Article How to Recall a Function in Python S srishti verma Follow Improve Article Tags : Python Practice Tags : python Similar Reads How to call a function in Python Python is an object-oriented language and it uses functions to reduce the repetition of the code. In this article, we will get to know what are parts, How to Create processes, and how to call them.In Python, there is a reserved keyword "def" which we use to define a function in Python, and after "de 5 min read How to Define and Call a Function in Python In Python, defining and calling functions is simple and may greatly improve the readability and reusability of our code. In this article, we will explore How we can define and call a function.Example:Python# Defining a function def fun(): print("Welcome to GFG") # calling a function fun() Let's unde 3 min read How to Call Multiple Functions in Python In Python, calling multiple functions is a common practice, especially when building modular, organized and maintainable code. In this article, weâll explore various ways we can call multiple functions in Python.The most straightforward way to call multiple functions is by executing them one after a 3 min read How to Add Function in Python Dictionary Dictionaries in Python are strong, adaptable data structures that support key-value pair storage. Because of this property, dictionaries are a necessary tool for many kinds of programming jobs. Adding functions as values to dictionaries is an intriguing and sophisticated use case. This article looks 4 min read How to Recall a Function in Python In Python, functions are reusable blocks of code that we can call multiple times throughout a program. Sometimes, we might need to call a function again either within itself or after it has been previously executed. In this article, we'll explore different scenarios where we can "recall" a function 4 min read How to use Function Decorators in Python ? In Python, a function can be passed as a parameter to another function (a function can also return another function). we can define a function inside another function. In this article, you will learn How to use Function Decorators in Python. Passing Function as ParametersIn Python, you can pass a fu 3 min read Like