- What should I include in C++ programs,
stdio.horcstdio? 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?
What should I include in C++ programs, stdio.h or cstdio ? and Why? Why
Share
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
cstdioand fully qualify the symbol names or else bring them in scope withusingdeclarations. This guarantees all symbols we use are present instdnamespace 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 useusingdeclarations.Note that we consider
stdio.hvs.cstdiohere just a sample use case, in practice it applies toallmostcxxxxandxxxx.hheaders, except a few like<math.h>and<cmath>.