C/C++ for the Python Developer: Introduction

Jonathan Hoffstadt
ITNEXT
Published in
5 min readAug 15, 2020

--

The first article in a series on bringing Python developers into the world of C/C++.

As a developer who works with both python and C/C++, I am confident in saying that when these languages are used together, they can amplify each of their strengths while avoiding their inherit weaknesses. Python provides a seemingly endless supply of powerful libraries, community support, and a relatively shallow learning curve. C/C++ on the other hand provides high performance, complete control, and the ability to interface with most other languages and libraries. While this may seem obvious to other developers working in both languages, a quick online search will reveal what seems to be a rift between python and C/C++ developers, especially with beginners.

C/C++ from the Python Point of View

For many python developers, C/C++ can seem extremely daunting and foreign. They are used to hearing their fellow programmers talk about its difficulty, complexity, and maybe even its age. Even when python programmers agree that C/C++’s speed blows python out of the water, its quickly downplayed by claiming that development time is significantly greater when compared to python. Not to mention claims that the language is dated and not “modern”.

Python from the C/C++ Point of View

For many C/C++ developers, python can seem like a toy. They are used to hearing other programmers talk about its speed(or lack thereof), simplicity, and weird syntax. Even when C/C++ programmers agree that python can do incredible things, it’s downplayed with claims such as: “its not real programming”, “that’s only possible because of xyz library”, and “it’s too slow”.

Another Point of View

Although I am no expert, I believe there are truths to both sides. However, these languages can be used in unison to overcome their weaknesses and highlight their strengths. This is what this series is about.

The target audience for this series is python developers looking to delve into the world of C/C++. I am assuming that the reader has a firm grasp on python basics such as: variables, functions, classes, the module importing system, etc. From these basics, I plan on teaching C++ using python as the frame of reference. The first milestone in the series will be a working knowledge of C++; the second milestone will be the ability to extend, embed, and possible contribute to python itself.

First Milestone: A working knowledge of C++

Our first milestone is the hardest. When compared to python, C++ is undeniably more complex with more rules, features, and less protection. Writing in python is riding a bike with a helmet, kneepads, training wheels, a worrying parent guiding you, and your in the grass. Writing in C++ is like having to build the bike before you can ride it.

So, what would a working knowledge of C++ look like, or better yet what is this series going to cover? Everything from how the C++ linker and compiler works all the way through variadic templates, pointers, threads, move semantics, and everything in between. By the end of this milestone, you will be able to not only create C++ programs and libraries, but also understand how C++ works “under the hood”.

Second Milestone: Extending/Embedding the Python Interpreter

Assuming you reached the first milestone, the second will be a breeze. Through this milestone you will gain a much deeper understanding of python and how it works. You’ll learn how to add new built-in types, add C extensions, and even embed the python interpreter into a larger C/C++ program. It’s through these skills that you’ll be able to truly utilize each languages strengths while avoiding their weaknesses.

Getting Started

At this point in the series, we will stray away from IDEs and platform specific details. Later in the series, we will switch to an actual IDE. Then after we cover some of the basics, we will begin talking about how the C++ linker and compiler works and how they differ from the python interpreter before going further into the C++ language.

For the first few articles I recommend using https://www.onlinegdb.com/ for both python and C++ code. The website is free and you can switch between various languages. We will be using Python3 and C++17.

Beating a Dead Horse: Hello World

As of course is tradition, I will start off with the hello world example:

Output:

Hello World

There are only 3 things to take note of at this moment:

  1. Statements end with a semicolon instead of a new line.
  2. To print to the console in C++, you must have #include <iostream> at the top of the file and printing to the console is done by calling std::cout<< x where x is the item you’d like to print.
  3. Unlike python, every C++ program must have an entry point which is a function called main which returns an int. We will be covering functions and types in the next article.

Although this example is trivial, there is actually a lot more going on here that we will cover later. For example what IS std::cout? And what’s with the << syntax? What is #include <iostream> and is it the same as importing a python module? These questions will be answered in upcoming articles.

Summary

Now that the introductory stuff is out of the way. We can start the fun stuff... in the next article. In the next article we will begin breaking down the type system and functions. If you are interested in this series and its direction, reach out!

Thanks for reading.

--

--

Writer for

Mechanical engineer and software developer. Maintainer and core developer of DearPyGui.