Preprocessor
This is how you solve the different parts for each platform. I don’t know how well you know the languages, but this is part of the preprocessor.
These preprocessor directives help translate the code even before the program builds, so everything that is in one #if
(which is not part of the program construction) of that platform is simply ignored even by the compiler. This translation is a simple form of code modification. Although it is part of the build process of the program and is done by the compiler it is not exactly the build process itself that deals with it. Hence there are even some criticisms that its use can bring problems since this translation is "dumb", does not understand much what you are doing. But it works well when the programmer knows how to use it correctly and there is no universally better solution.
This makes the build faster (has less code to compile effectively), and evidently the generated machine code gets smaller and faster, after all it does not need to decide at runtime whether it is relevant to that platform or not.
Probably the alternative would be to solve this at runtime somehow. It won’t look good for most programs. At least the program will be bigger without need, after all there are things in it that will never be used on that platform where it is running (remember that a program generated for one platform does not run on another). Depending on the technique used to select which code to execute can even get slower (theoretically, because no one will get to this point).
This goes for C and C++. C++ is essentially C with a few more features (I’ll leave out some irrelevant details for this question). These features can greatly decrease the need for preprocessor use but in this case selecting the platform specific code has nothing better.
Of course, you don’t know this, but your question doesn’t seem to make much sense. One of the reasons you might think this is that you don’t have much experience with both so understand:
Codes can be more or less portable.
C++ has more features. Therefore, it has more chances of them being misused, making portability difficult. But this is the problem of code, of programmer, not of language.
Languages at most can have more or less facilities to separate codes for different platforms (not the case of C++ over C).
In terms of having more features that help portability, even those that go beyond the pre-processor, if you have something else, it’s very little. It would have to make creative use of a few things. It will often bring more problems than benefits.
It is possible to use templates, for example, but it will hardly bring any real advantage. I don’t see this being used in the codes I usually read (which is different than not existing).
Language implementations can meet more or less platforms.
In practice it is often said that C++ is slightly less portable because there are no good implementations for all platforms. But there are controversies about it being an absolute truth.
Library is something different from language
What surely C++ has the most by default, mainly C++11, C++14, C++17, etc., are libraries that already handle the different platforms for you. But it is not a language resource. Then you will see how these libraries were written and you will probably find a lot of #if
there. That is, the advantage is that someone has already done for you.
Of course C has libraries that deal with platform differences and facilitate the life of the programmer, but little is in the library that is considered as standard language.
Someone might come here and reply that it makes a lot of difference for portability to use C or C++. This can happen because I don’t know any little technique or because some people like to use a certain resource so much that they prefer it more than any other. There are people who have so much hatred to the use of the pre-processor that they may prefer all the difficulties of the template, for it will be easier (the best tool is the one you know and get along with).
Both have another problem that hinders portability between language versions or libraries or even implementations (compilers). Probably C++ suffers more from it, but I believe that’s not quite what you’re asking.
And have you been reading C++ codes and not finding the same structure? Have you ever wondered if this is just a matter of style?
– Maniero
I actually read a few codes in C++, but I was quite intrigued by why they did this kind of #if since the early, unproporting versions of the code had none of that.
– jlHertel