When using C headers in C++, should we use functions from std:: or the global namespace?

From the C++11 Standard (emphasis mine):

D.5 C standard library headers [depr.c.headers]

  1. For compatibility with the C standard library ...
  2. Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope (3.3.6) of the namespace std and are then injected into the global namespace scope by explicit using-declarations (7.3.3).
  3. Example: The header <cstdlib> assuredly provides its declarations and definitions within the namespace std. It may also provide these names within the global namespace. The header <stdlib.h> assuredly provides the same declarations and definitions within the global namespace, much as in the C Standard. It may also provide these names within the namespace std.

Using the «name.h» headers is deprecated, they have been identified as candidates for removal from future revisions.

So, I would suggest to include the «cname» headers and to use the declarations and definitions from the std namespace.

If you have to use the «name.h» headers for some reasons (it's deprecated, see above), I would suggest to use the declarations and definitions from the global namespace.

In other words: prefer

#include <cstdio>

int main() {
    std::printf("Hello world\n");
}

over

#include <stdio.h>

int main() {
    printf("Hello world\n");
}

<cmeow> always provides ::std::purr and may or may not provide ::purr.

<meow.h> always provides ::purr and may or may not provide ::std::purr.

Use the form that is guaranteed to be provided by the header you include.


No, you're fine either way.

The original intent was that the <___.h> headers would be the C versions which put everything in the global namespace, and the <c___> headers would be the C++-ified versions, which place everything in the std namespace.

In practice, though, the C++ versions also put everything into the global namespace. And there's no clear consensus that using the std:: versions is "the right thing to do".

So basically, use whichever you prefer. The most common is probably to use the C standard library functions in the global namespace (printf instead of std::printf), but there's not much reason to consider one "better" than the other.