C Library Wrappers
The C++ library includes the entire C standard library (from
the 1990 C standard, plus Amendment 1), in which each C header, such as
<stdio.h>
, is wrapped as a C++
header (e.g., <cstdio>
). Being
part of the C++ standard, all types, functions, and objects are declared
in the std
namespace.
The external names are also reserved in the global namespace.
Thus, proper practice is to use the names in the std
namespace (e.g., std::strlen
), but realize that these names are
also reserved in the global namespace, so you cannot write your own
::strlen
function.
The C standard permits macros to be defined to mask function
names. In the C++ wrappers for these headers, the names must be declared
as functions, not macros. Thus, the C <stdio.h>
header might contain the
following:
extern int printf(const char* fmt, ...); #define printf printf
In C++, the printf
macro is not permitted, so the <cstdio>
header must declare the
printf
function in the std
namespace, so you can use it as std::printf
.
A deprecated feature of C++ is that the C standard headers
are also available as their original C names (e.g., <stdio.h>
). When used in this fashion,
their names are in the global namespace, as though a using
declaration were applied to each name
(e.g., using
std::printf
). Otherwise, the old style headers
are equivalent to the new headers. The old C header names are
deprecated; new code should use the <cstdio>
, etc., style C headers.
Get C++ In a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.