Why choose C instead of C++ or else C++ instead of C?

I think everyone with minimal knowledge knows when to choose C or C++ over other languages. There are clear cases where these languages are more suitable and others that do not make so much difference. But there may be a situation where you know that one of the two is ideal but you don't know which one of the two.

What concrete elements should we analyze and in what situations can we be sure that C is more indicated than C++ or C++ is the most suitable?

The reasons don't have to be essentially technical but they have to be objective, it has to be something experienced professionals in both languages usually agree on.

In other words, which problems does C handle best and which ones does C++ usually do well?

Of course both can be used for any project, but it is not always the most suitable tool. There are cases that some requirements may make a choice unviable. 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.

 83
c++
Author: Maniero, 2014-06-03

5 answers

I will answer the question by pointing out some facts and advantages about languages. Make the choice on your own:

  • C can be compiled into any architecture.

    One of the first things that happens when a new architecture emerges is the creation of a compiler for the C language (perhaps based on an existing compiler, just adding a new target). The point is that whatever architecture you find, it's virtually guaranteed that you can write a program in C and compile in it. This is because C is a "simple" and fairly low-level language. C++ is immensely more complex and is likely to come across architectures that do not yet have support for the language.

    However it must be said that this difference between languages is less and less significant as compiler collections become more widely used. For GCC or Clang (LLVM), by for example, you can add a new backend that compiles the specific intermediate representation of compilers for your target architecture and will immediately have support for all languages (frontend) that the compiler supports. When it comes to microcontrollers, GCC supports many of their architectures, and it is perfectly acceptable to write a program in C++ instead of the usual C.

  • C is faster than C++

    Lie! This is one more myth history what else. The fact is that C++ compilers are much more complex than C compilers, so it's hard to apply optimizations to transformations that benefit speed. But today's compilers are much better than yesterday's compilers and this difference is largely overcome. The affirmative actually reverses. C++ has features that allow you to write code more efficiently than in C while maintaining the elegance . Note templates and functions constexpr 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::regex instead of writing a parser that processes a const char* in hand. But everything that can be written in C, can be written in C++ with exactly the same efficiency. There are only pluses.

  • C has a well-defined ABI within each system

    Both or Linux how much Windows define an API in C for applications to use. Along with this, they also define an ABI, an assembly way to pass arguments to these system functions and name them. Therefore, any compiler will use the same mechanism as the operating system to write the passage of arguments and the nomenclature of the internal functions of the code. This means that given any binary library, I can load a symbol and call it as a function with the confidence that I'm doing it right.

    Already in C++ there is no well-defined convention as to 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. Thus it is 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 C++, but exporting an interface in the formed of C).

  • 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 output is perfectly acceptable. Note 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 Assembly in the style of the LLVM (it is not as low level as the assemblies for real machines) and it produces an executable on the target platform. That's what Clang does.

  • C++ provides zero-cost features

    A C++ motto is zero overhead, zero overhead. If you do not use a certain resource, you do not pay for it. This principle ensures that the language is full of features, but at the same time it is as fast as you want. Just avoid some key points, like RTTI (run-time type identification ), polymorphism or exceptions. The language can also work without the operating system, just do not use the standard library and write everything on your own.

  • C compilers are faster and spend less memory

    True. In general, compiling a program in C is faster because it is a simpler language. The main reason for C++ code being heavier to compile is the templates. They produce large amount of additional specialized functions that need to receive inline and be optimized. In addition to calculations that happen at compile time via constant expressions. Anyone who has compiled something that includes boost will say so. Be that as it may, This is rarely a decisive reason to choose one language over the other, but a point to consider in very large projects.

  • C is the most used in open source projects

    This is one more factor primarily historical. In 20 or 30 years, when most of the big projects we see today were crawling, C++ was not as popular and C++ compilers were much inferior to C's in quality and performance issues. 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 much cleaner, modularized and organized code, the advantages there are many. However there is still a large and irrational resistance to the use of C++.

  • C is obsolete. C++ is the substitute.

    Another misconception. 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 modern. As an example, in 2011 both have gained native thread support. Another point to leave of course, not every C code is valid C++ code, especially the conversion between pointer types. In C there are even features that have not yet reached C++, such as the restrict keyword, mentioned by @luiscubal in the comments, although most compilers support it as an extension.

 74
Author: Guilherme Bernal, 2018-05-28 20:17:04

C and C++ belong to different architectures *

Despite the possibility of using the C++ structural paradigm( c++ is multi-paradigm), however it is not possible to use the OO paradigm (in all its amplitude because it is possible to store a pointer to a function within a struct( making a half-forged object)) in a program written exclusively in C.

There are a lot of APIs dedicated to providing an interface with C++

There are a lot of APIs dedicated to providing an interface with C++, from my own experience I have had to use APIs aimed exclusively at C++ (and not C) like that of Tesseract OCR.

C++ is much more powerful than C

C++ is much more powerful than C in terms of functionality due to the fact that it is an extension of C Wikibooks , author Robert W. Sebesta in his book of programming language concepts exposes the range of features more than C++ has (even in structural paradigm).

C is a bit faster than C++

It is possible to notice a slight extra performance of programs written in C, comparative benchmarks can be found in benchmark.

C and C++ are at different levels * Some authors consider C a mid-level language while C++ is considered self-level because it has object orientation, has more advanced and a greater distance from the machine, here comes a note se C++ é um superconjunto de C então ela também pertence ao mid-level.

 14
Author: Ricardo, 2015-04-21 23:25:46

I understand that there are two main factors that affect the choice:
1) the libraries to be used in the project, and;
2) language mastery;

Libraries are a determining factor. Many projects use ready-made libraries and must include third-party class declaration modules. So it is more practical that the project is developed in C++. To isolate access to objects defined in classes there will be an extra effort.
In the case of development in Windows environment, using access to modules with Com/ActiveX interface does not make sense to use ANSI C, since the work to adapt the calls to the requirements of this technology will require great technical difficulties.

Driver development has factors that make it suitable to use C. Its features allow for example that your code is contained strictly in your source, with dependencies limited to System API calls. Modules with minima external dependencies tend to be candidates for using C as a development language. Many small embedded systems use C-restricted compilers, forcing their choice.

Feeling comfortable when using a language is a determining factor. Therefore mastering technology affects the choice of language. Some facilities offered by C++ are in my view very attractive compared to pure C. Be able to declare variables at any point in the code or ensuring resource release using classes are a few.

 6
Author: lsalamon, 2014-06-04 19:36:51

My answer to your question would be:

Only use C++

With the following caveat: unless there is any reason why you are obliged to use C...

What Would these reasons be?

  1. the lack of C++ compiler for the target platform;
  2. restrictions on the target platform, such as problems implementing exception handling; or
  3. lack of knowledge of the C++language.

There must be remember that C++ is almost a superset of the C language, that is, virtually all programs or libraries written in C will run unchanged in C++, generating exactly the same code.

 3
Author: Massa, 2014-06-04 19:18:36

In my opinion and being very direct:

C you use when you need something lower level, something closer to machine language. When you need to use the machine's resources more specifically to your problem, as in an embedded project for example.

C++ is a high-level language with the highest can of abstraction. With it it is possible to use object orientation, which facilitates the development of the system. Often Systems everyday common

Hope I helped ;)

 -1
Author: Alysson Chicó, 2018-11-13 19:41:05