- What should I include in C++ programs,
stdio.h
orcstdio
? and Why? - Why two header files which provide the same functionality?
- What does the standard say regarding this?
- How should I go about including other such headers, Is there a base rule that I should follow?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
Since this post is a bit old I wanted to share the following:
Looking at code:
They both compile and execute ok!
Which one is better in C++?
Regarding C++11's and C++17's specification:
Both C++11 and C++17 standard specifications documents state the use of
<X.h>
remains for compatibility with the C standard, although their use is regarded as deprecated.Regarding C++ 20 standard proposal
They are reviewing "undeprecating" the use of the C library headers in C++20.
<X.h>
appear highlighted in green. C++11 and C++17 deprecation, as of now, is stated as a "weak recommendation" and a "tweak" for keeping the "C standard library headers (c.headers)" is displayed below:So, it seems
<X.h>
aren't going anywhere. Ultimately, you can use both. Personally, I would make the decision of which one I would use boil down to having your code backwards compatible with C code or not.Consider the following programs:
Sample 1:
Sample 2:
Both work as expected. So which usage is more appropriate? The answer is: Neither! Surprised? Read on.
The C++ Standard library provides all standard C headers for compatibility reason, while C++ as a language also provides all the equivalent headers. As a convention,
cxxxxx
.The C++ Standard mentions this under Annex D (normative) Compatibility features:
§2 mentions the important distinguishing point. This rule applied to the examples above means:
Let us apply this rule to our sample codes and measure the pros and cons:
Sample 1: This brings all the symbols from stdio.h in the global namespace. Advantage is that you can use the symbols without any qualification since they are imported in the global namespace. Downside is that you end up polluting the global namespace with many symbol names that you will probably never use. This might lead to symbol name collision. In C++ always consider the global namespace as a minefield and avoid it as much as possible.
Sample 2: This is a very bad practice because there is no guarantee that the implementation will put the symbols in global namespace, the standard simply does not demand to do so. We are simply relying on the behavior of one particular compiler implementation. We cannot and should not assume that all compilers will do so. So strictly speaking the program is not standard approved and this usage is not portable across all implementations.
So what is the correct usage?
The correct usage is to use
cstdio
and fully qualify the symbol names or else bring them in scope withusing
declarations. This guarantees all symbols we use are present instd
namespace and we are not polluting the global namespace. Example of correct usage:Sample 3:
Note that the directive
using namespace std;
, especially in a header, is not a good option and you should always useusing
declarations.Note that we consider
stdio.h
vs.cstdio
here just a sample use case, in practice it applies toallmostcxxxx
andxxxx.h
headers, except a few like<math.h>
and<cmath>
.