C versus C++

Hello everyone,

please excuse me for asking a rather naive question.

My programming background is mostly limited to Python and JavaScript, both of which I know very well. Recently, I’m getting very interested in DSP and hardware projects. Today, I took a first plunge and read through a couple of guides to C for people who know Python and wrote my first program. In terms of the number of concepts, C actually looks surprisingly simple, and I think I can be friends with it.

However, rather than relying on C, from what I can tell, Daisy (e.g. libDaisy) as well as many DSP-based Eurorack modules (e.g. the Mutable Instruments) use C++. I was surprised to find that a lot of people rant heavily about C++.

This made me wonder:

  1. Why does e.g. libDaisy use C++ rather than C, i.e., which language features motivated that choice? Is it about classes, or does it come down to other reasons?
  2. Do you have any pointers for books or tutorials to get started with C++ avoiding the traps that people criticize C++ for, to learn good practices from the beginning?

Cheers

You can do in C++ pretty much everything you can do in C. It allows you to use some higher level metaprogramming easier and safer (templates vs just macros). In case of embedded programming you will typically see a mix of C and C++, with a few features of C++ ignored (i.e. exceptions).

A good book to read would be " Real-Time C++: Efficient Object-Oriented and Template Microcontroller Programming".

1 Like

I also came from Python and I can recommend Bjarne Stroustrup’s: “A Tour of C++”
It’s a good introduction by the creator himself and teaches a lot of best practices.
Also if you wanna nerd on C++ I’d recommend the videos by Jason Turner.

For me the rant about C++ vs C is about the same as not using Python because “it’s slow”.
I mean, I value getting to a solution faster, making fewer stupid errors and writing less boilerplate code over a few milliseconds of execution time.
(And even that is not true for C++ as it will be compiled to almost the same as C).

There are definitely a lot of situations where you need to get the last bit of performance. That’s why Daisy or Teensy or any other embedded platform sprinkle in some C or even Assembler where its needed. Just like you would throw in a C/C++ extension in Python when things need to be faster.

1 Like

Thank you both for your helpful answers!

I picked up a copy of the Tour of C++ book and read through the first chapter today. So far, it has a good pace, so thanks a lot for the recommendation.

From a beginner’s perspective the world of C++ seems much more messy than C. Many different versions and overall much more concepts and features than C, which makes it seem a bit more daunting to get into. That is my main concern with it.

I guess once I get an overview and start reading e.g. through the libDaisy codebase it will become more apparent why it is written in C++ rather than C.

You are right about the versions of C++ being daunting at first.

But actually it just like Python versions. Anything above C++11 is fine. Think of it as Python 3.5

There is C++17 and C++20 but IIRC the STM libs and therefore Daisy are still on C++14.
Oh and the numbers are just the years of release. Used to be a three years release cycle.

1 Like

And you are also right about the daunting features and concepts. But if you stick with the tour book, you’ll get most of the stuff you will ever need.

Templates and constexpr are nice thou. :wink:

1 Like

I am coming at this from the hardware and DSP side. I have done a lot of assembly and “bare metal” C programming on small processors, and really see the value of a language allowing you to stay close to the hardware if you want efficiency. C++ adds more abstractions like classes, methods, and templates to C, but these don’t necessarily take you so far into the clouds that you can’t tell how they will run on the processor.

As another resource, I am watching this “C++ Programming in 10 hours” video on Youtube:

This guy is a bit of a goof, but he knows his stuff and is really quite a good teacher. The first 2/3rds of is is mostly standard C-type programming. Good review for me, and also for folks coming from other languages, IMO. (Watch at 1.5x playback speed.) The object oriented final 1/3rd is very well explained. He alternates between chalkboard conceptual descriptions, and writing and running example code in real-time. For me this has been much more effective than other C++ videos or web articles I have tried.

1 Like