The C++ standard library incorporates the C standard library (with a few minor tweaks).
Each C header with a name like <foo.h>
has a corresponding C++ header <cfoo>
. For example, the C++ header <cstdio>
corresponds to the C header <stdio.h>
.
Quoting the 2011 ISO C++ standard, 17.6.1.2 [headers] paragraph 4:
In the C ++ standard library, however, the declarations (except for
names which are defined as macros in C) are within namespace scope
(3.3.6) of the namespace std
. It is unspecified whether these names
are first declared within the global namespace scope and are then
injected into namespace std
by explicit using-declarations (7.3.3).
So given #include <cstdio>
, the printf
function definitely can be referred to as std::printf
, and optionally may be visible as printf
in the global namespace. (This option is up to the implementation, not the programmer.)
Of course you can refer to it as just printf
within the scope of using namespace std
.
In my opinion, this is unfortunate; it seems to be for the convenience of implementers rather than programmers. It's safest to assume that printf
is declared only with in the std
namespace. If you use #include <cstdio>
and then refer to printf
in the global namespace, your code might compile today and fail to compile on a different implementation.
Conversely, as a deprecated feature, the C++ standard library also includes the C standard headers with their original names, such as <stdio.h>
. Quoting the standard, section D.5 [depr.c.header]:
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).
So given #include <stdio.h>
, the name printf
is definitely visible in the global namespace, and optionally (again, this is the implementation's option, not yours) visible as std::printf
.