I think everyone with the slightest bit of knowledge knows when to choose C or C++ over other languages. There are clear cases in which these languages are more suitable and others that don't make much difference. But there may be situations where you know that one of the two is ideal but you don't know which one.
What concrete elements should we analyze and in which situations can we be sure that C is more suitable than C++ or C++ is more suitable?
The reasons don't have to be essentially technical but they need to be objective, it needs to be something that experienced professionals in both languages tend to agree.
In other words, which issues does C handle best and which issues does C++ typically do well?
Of course, both can be used for any project, but it's not always the most suitable tool. There are cases where some requirements can make a choice unfeasible. What requirements can make it so difficult to use one language that it is better to use the other?
Consider political points as not fundamental requirements, only the technical part really matters.
I will answer the question by pointing out some facts and advantages about languages. Make the choice yourself:
C can be compiled on any architecture.
One of the first things that happens when a new architecture comes along is the creation of a compiler for the C language (perhaps building on an existing compiler, just adding a new target ). The point is that whatever architecture you come across, it's pretty much guaranteed that you can write a C program and compile on it. This is because C is a "simple" and very low-level language. C++ is immensely more complex, and it is likely that you will encounter architectures that do not yet support the language.
However, it must be said that this difference between languages is less and less significant as collections of compilers become more widely used. For GCC or Clang (LLVM), for example, you can add a new backend that compiles the compilers-specific intermediate representation for your target architecture and will immediately support all languages (frontend) that the compiler supports. When it comes to microcontrollers, GCC supports many of their architectures, making it perfectly acceptable to write a program in C++ instead of the usual C.
C is faster than C++
Lie! This is a more historical myth than anything else. The fact is that C++ compilers are much more complex than C compilers, so it's difficult to apply optimizations to transformations that benefit speed. But today's compilers are much better than yesterday's compilers and that difference is largely overcome. The statement is actually inverted. C++ has features that allow you to write code more efficiently than C while maintaining elegance . Take constexpr templates and functions as an example. But of course, in C++ you have a lot of high-level features that would be less efficient than dealing with the simplest implementation. Of course, it's less efficient to use
std::regexinstead of writing a parser that processes a
const char*by hand. But everything that can be written in C can be written in C++ with exactly the same efficiency. There are only advantages.
C has a well-defined ABI within each system
Both Linux and Windows define an API in C for applications to use. Along with that, they also define an ABI, an assembly-based way of passing arguments to these system functions and naming them. Therefore, any compiler will use the same mechanism as the operating system to write the passing of arguments and the naming of the code's internal functions. This means that given any binary library, I can load a symbol and call it like a function with the confidence that I'm doing it right.
In C++, however, there is no well-defined convention for this, each compiler uses a different ABI and it is not difficult to see different versions of the same compiler using a different ABI to accommodate some new feature. So it's much more complicated to write an interface for C++. Most scripting languages, for example, only accept native extensions that have been written in C (or in C++, but exporting an interface in C format).
C is universal
You can compile C for almost any architecture. In any environment you will have a stable ABI. So, if you want to make a tool that generates portable code, using C as the output is perfectly acceptable. Take the bison as an example. Still, I would advise you to take a look at what LLVM has to offer in this regard. You generate LLVM-style assembly (not as low-level as assemblies for real machines) and it produces an executable on the target platform. That's what Clang does.
C++ provides features at zero cost
A C++ motto is zero overhead , zero overhead . If you don't use a certain feature, you don't pay for it. This principle ensures that the language is full of features, but at the same time, it's as fast as you want it to be. Just avoid some key points, such as RTTI ( run-time type identification ), polymorphism or exceptions. The language can also work without the operating system, just don't use the standard library and write everything on your own.
C compilers are faster and use less memory
Truth. In general, compiling a program in C is faster because it is a simpler language. The main reason C++ code is heavier to compile is templates. They produce loads of additional specialized functions that need to be inlined and optimized. In addition to the calculations that happen at compile time via constant expressions. Anyone who has compiled something that includes boost says so. In any case, this is rarely a decisive reason for choosing one language over another, but a point to consider in very large projects.
C is most used in open source projects
This is another primarily historical factor. 20 or 30 years ago, when most of the big projects we see today were in their infancy, C++ was not that popular and C++ compilers were much inferior to C in terms of quality and performance. So going to C was a natural choice. And today, converting a project of this size to a different language is a monumental task, but not impossible . With C++ it is possible to write code much cleaner, modularized and organized, the advantages are many. However, there is still great and irrational resistance to using C++.
C is obsolete. C++ is the replacement.
One more mistake. C and C++ are different languages, although they share parts in common. Each has its use. But none of them are dead. New revisions and updates are created regularly, keeping them up to date. As an example, in 2011 both gained native thread support. Another point to make clear is that not all C code is valid C++ code, especially the conversion between types of pointers. In C there are even features that haven't made it to C++ yet, like the
restrictkeyword mentioned by @luiscubal in the comments, although most compilers support it as an extension.