Just pass regular Python code as the argument to the flag:

python -c 'print 1
print 2'

Import modules works, and blank lines are OK, too:

python -c '
import pprint
pprint.pprint(1)
'

When using this feature, just be mindful of shell quoting (and indentation), and keep in mind that if you're using this outside of a few shell scripts, you might be doing it wrong.

Answer from Thomas Orozco on Stack Overflow
🌐
Real Python
realpython.com › c-for-python-programmers
C for Python Programmers – Real Python
March 18, 2026 - Note: Boolean variable types are supported and used in parts of the CPython source, but they aren’t part of the original language. C interprets binary conditions using a simple rule: 0 or NULL is false, and everything else is true. Unlike Python, C also supports switch.
🌐
Python
docs.python.org › 3 › extending › extending.html
1. Extending Python with C or C++ — Python 3.14.7 documentation
The C extension interface is specific to CPython, and extension modules do not work on other Python implementations. In many cases, it is possible to avoid writing C extensions and preserve portability to other implementations. For example, if your use case is calling C library functions or system calls, you should consider using the ctypes module or the cffi library rather than writing custom C code.
Discussions

How to use the -c flag in python - Stack Overflow
I noticed in the python doc that there is a -c flag. Here is what python doc says: Execute the Python code in command. command can be one or more statements separated by newlines, with signific... More on stackoverflow.com
🌐 stackoverflow.com
How are Python and C related? I've read that Python is 'made from C'. Does that mean that Python is just an abstraction of lots of large C functions?
Python runs on an interpreter* called CPython. The interpreter takes in Python code (,compiles it into an internal representation) and runs it. CPython, as the name suggests is written in C. So, when you run a Python script like "print(5+5)", CPython, the program you usually run with the python command, reads the line, lexes it into Tokens like print, (, 5, +, 5, ) then transforms it a couple more times, until it ends up with a bytecode representation, something like: LITERAL 5, LITERAL 5, ADD, PRINT, where LITERAL pushes the number 5 onto the "stack", ADD takes the two items highest on the stack, removes them from it, and pushes their sum back onto it and PRINT removes the top item from the stack and prints it. So it works like this: INSTRUCTION: {} STACK: [] INSTRUCTION: LITERAL STACK: [5] INSTRUCTION: LITERAL STACK: [5, 5] INSTRUCTION: ADD STACK: [10] INSTRUCTION: PRINT STACK: [] stdio: "10" It's not an abstraction in C per se, as in, a C compiler can be not involved in the life of a Python program. The interpreter is compiled on someone else's computer, you can download it, and it will work without a C compiler. CPython doesn't compile Python to C, so there never is a point where your python program is a C program. *- that's the most commonly used implementation anyway More on reddit.com
🌐 r/learnprogramming
25
70
August 29, 2022
Understanding what CPython actually IS has greatly enhanced my understanding of Python.
Potential errors just happen as it goes through each line of code, one by one. However its also important to understand that Python is actually still semi-compiled into "bytecode", I think it's helpful, and not too difficult, to notice the difference between run-time and compile-time errors. You could try print("hello") 5/0 print("world") vs the same thing with a tiny addition print("hello") 5/0 print("world") ( In the first example we'll print once before hitting a ZeroDivisionError, which is what people imagine when they imagine interpreting line by line. In the second example we'll print nothing, because we hit a SyntaxError before ever starting to run. Even though the error is "after" the second print, it's a different kind of error than what we saw previously. Of course real errors are more complex, but you can benefit from knowing which ones occur in the course of a live program vs which ones prevent the program from living. More on reddit.com
🌐 r/learnpython
35
145
May 25, 2024
Can we use C code in Python? - Stack Overflow
I know that Python provides an API so you can call Python interpreter in C code, but what I want is the opposite. My program needs to use some C API, so the code must be written in C. But I also w... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Python
docs.python.org › 3 › c-api › index.html
Python/C API reference manual — Python 3.14.7 documentation
This manual documents the API used by C and C++ programmers who want to write extension modules or embed Python. It is a companion to Extending and Embedding the Python Interpreter, which describes...
Top answer
1 of 12
159
Python runs on an interpreter* called CPython. The interpreter takes in Python code (,compiles it into an internal representation) and runs it. CPython, as the name suggests is written in C. So, when you run a Python script like "print(5+5)", CPython, the program you usually run with the python command, reads the line, lexes it into Tokens like print, (, 5, +, 5, ) then transforms it a couple more times, until it ends up with a bytecode representation, something like: LITERAL 5, LITERAL 5, ADD, PRINT, where LITERAL pushes the number 5 onto the "stack", ADD takes the two items highest on the stack, removes them from it, and pushes their sum back onto it and PRINT removes the top item from the stack and prints it. So it works like this: INSTRUCTION: {} STACK: [] INSTRUCTION: LITERAL STACK: [5] INSTRUCTION: LITERAL STACK: [5, 5] INSTRUCTION: ADD STACK: [10] INSTRUCTION: PRINT STACK: [] stdio: "10" It's not an abstraction in C per se, as in, a C compiler can be not involved in the life of a Python program. The interpreter is compiled on someone else's computer, you can download it, and it will work without a C compiler. CPython doesn't compile Python to C, so there never is a point where your python program is a C program. *- that's the most commonly used implementation anyway
2 of 12
14
Python and C are not directly related. The official Python interpreter named CPython is written in C, but that has nothing to do with the Python language itself. Other people have written other Python interpreters in other languages.
🌐
InfoWorld
infoworld.com › home › software development › programming languages › python
PythoC: A new way to generate C code from Python | InfoWorld
December 19, 2025 - A new project, PythoC, takes a different approach. It uses type-hinted Python to programmatically generate C code—but chiefly for standalone use, and with many more compile-time code generation features than Cython has.
🌐
Wikipedia
en.wikipedia.org › wiki › CPython
CPython - Wikipedia
2 weeks ago - This can happen when multiple threads are servicing separate clients. One thread may be waiting for a client to reply, and another may be waiting for a database query to execute, while the third thread is actually processing Python code.
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › understanding what cpython actually is has greatly enhanced my understanding of python.
r/learnpython on Reddit: Understanding what CPython actually IS has greatly enhanced my understanding of Python.
May 25, 2024 -

First off, its perfectly understandable to not really care about language theory as a beginner. This stuff is not necessary to learn to code.

However, after recently doing some deep dives on what CPython really is and how it works, I have found the knowledge to be extremely enlightening. And it has really opened my eyes as to how Python is used, and why its used in the places it is.

For those who are unaware, allow me to share what I've learned.

So the key piece of information is that CPython is, at its core, a program written in C. Its purpose is to take Python code as input, then convert that Python into its own native instructions (written in C), and then execute them. And perhaps most importantly, it does this in a line-by-line manner. That just means it doesn't try to error check the entire program before running it. Potential errors just happen as it goes through each line of code, one by one.

However its also important to understand that Python is actually still semi-compiled into "bytecode", which is an intermediate stage between Python and full machine code. CPython converts your python scripts into bytecode files first, so what it actually runs is the bytecode files.

Now where it gets super interesting is that CPython is not the only "implementation" of Python (implementation means some kind of program, or system, that takes Python code as input and does something with it). More on that later.

On the subject of bytecode, it naturally leads to some other interesting questions, such as "Can I share the bytecode files?", to which the answer is no. That's one of the key aspects of CPython. The bytecode is "not platform agnostic". (I'm really sorry if that's not the correct term, I just learned all this stuff recently). That means the bytecode itself is compiled for your specific environment (the python version and dependencies). The reason for this is that its part of Python's design philosophy to be constantly improving the bytecode.

Once you understand that you can then comprehend what other implementations of Python do. PyPy for instance aims to make a Python running environment that works more like Java, where it performs "just-in-time" compilation to turn the bytecode into native machine code at runtime, and that's why it can make certain things run faster. Then you have the gamut of other ways Python can be used, such as:

  • Cython - aims to translate Python into C, which can then be compiled

  • Nuitka - aims to translate Python into C++, which is more versatile and less restrictive

  • Jython - this semi-compiles Python into Java bytecode that can be run in a Java virtual machine/runtime

  • IronPython - semi-compiles Python into C# bytecode, for running in .NET runtime

  • PyPy - A custom JIT-compiler that works in a manner philosophically similar to Java

  • MicroPython - a special version of python that's made for embedded systems and 'almost' bare-metal programming

Oh and then there's also the fact that if you want to use Python for scripting while working in other languages, its important to understand the difference between calling CPython directly, or using "embedded" CPython. For instance some game coders might opt to just call CPython as an external program. However some might opt to just build CPython directly into the game itself so that it does not need to. Different methods might be applicable to different uses.

Anyway all of this shit has been very entertaining for me so hopefully someone out there finds this interesting.

🌐
Cornell Virtual Workshop
cvw.cac.cornell.edu › python-performance › intro › cpython-api
Cornell Virtual Workshop > Python for High Performance > Overview > CPython and the Python/C API
CPython, which is written in C, ... other language that C can interoperate with). The Python/C API allows for compiled pieces of code to be called from Python programs or executed within the CPython interpreter....
Top answer
1 of 2
128

I want to invoke those C function or executables in python. Is that possible.

Yes, you can write C code that can be imported into Python as a module. Python calls these extension modules. You can invoke it from Python directly, an example from the documentation:

Python Code

import example
result = example.do_something()

C Code

static PyObject * example(PyObject *self)
{
    // do something
    return Py_BuildValue("i", result);
}

If I want the C code to be a library, which means I use it with #include and linkage of the *.o likely in python, how to do it or is that possible.

You build it as a shared library *.dll or *.so You can also investigate using distutils to distribute your module.

If I write the C code into executable, which means it becomes a command, can I invoke it in python directly?

If you write a *.exe then you are doing the opposite (invoking Python from C). The method you choose (exe vs shared library) depends on if you want a "C program with some Python" or a "Python program with some C".

Also, I heard that python code can be compiled, does that mean we can execute the code without the source file? Are the output files binary files? Does it improve performance?

Python reads *.py files and compiles to *.pyc bytecode files when you run it. The bytecode is then run in the Python virtual machine. This means "executing the same file is faster the second time as recompilation from source to bytecode can be avoided." (from the Python glossary) So if you haven't edited your *.py files, it will run the *.pyc. You can distribute *.pyc files without *.py files, however they are not encrypted and can be reverse-engineered.

2 of 2
16

You don't necessary need to extend Python (which is not trivial, btw), but can use foreign function interface such as ctypes.

🌐
Python
python.org › about
About Python™ | Python.org
The Python Package Index (PyPI) hosts thousands of third-party modules for Python. Both Python's standard library and the community-contributed modules allow for endless possibilities.
🌐
GitHub
github.com › python › cpython › blob › main › Programs › python.c
cpython/Programs/python.c at main · python/cpython
#include "Python.h" · #ifdef MS_WINDOWS · int · wmain(int argc, wchar_t **argv) { return Py_Main(argc, argv); } #else · int · main(int argc, char **argv) { return Py_BytesMain(argc, argv); } #endif
Author: python
🌐
GeeksforGeeks
geeksforgeeks.org › python › using-c-codes-in-python-set-1
Using C codes in Python | Set 1 - GeeksforGeeks
July 11, 2025 - Using ctypes : Python ctypes will come to play but make sure the C code, that is to be converted, has been compiled into a shared library that is compatible with the Python interpreter (e.g., same architecture, word size, compiler, etc.). Further the libsample.so file has been placed in the same directory as the work.py.
🌐
GitHub
github.com › cython › cython
GitHub - cython/cython: The most widely used Python to C compiler · GitHub
The most widely used Python to C compiler. Contribute to cython/cython development by creating an account on GitHub.
Author: cython
🌐
University of Toronto
cs.toronto.edu › ~patitsas › cs190 › c_for_python.html
C for Python Programmers
One major difference between C and Python is simply how you go about executing programs written in the two languages. With C programs, you usually use a compiler when you are ready to see a C program execute; by contrast, with Python, you typically use an interpreter.
🌐
CodeConvert AI
codeconvert.ai › home › convert from python › python to c converter
Free Python to C Converter - AI Code Translation | CodeConvert AI
A well commented function to check if a number if odd or even. ... Paste your Python code into the input box, confirm the languages are set to Python and C, and click Convert.
🌐
Python Tips
book.pythontips.com › en › latest › python_c_extension.html
22. Python C extensions — Python Tips 0.1 documentation
Just because you want to. The Python ctypes module is probably the easiest way to call C functions from Python. The ctypes module provides C compatible data types and functions to load DLLs so that calls can be made to C shared libraries without having to modify them.
🌐
Readthedocs
reptate.readthedocs.io › developers › python_c_interface.html
Tutorial: Interfacing Python and C code — RepTate 1.4.0 documentation
Fortunately, there are many solutions available to write code that will run fast in Python. We can cite Cython or Numba that transform Python code into C executable and require minimal addition to the existing Python code. There is also Ctypes that provides C compatible data types, and allows calling functions from external libraries, e.g.