|
||||
|
Section 32:
|
[32.2] How can I include a standard C header file in my C++ code?
To #include a standard header file (such as <cstdio>), you don't have to do anything unusual. E.g.,
// This is C++ code
#include <cstdio> // Nothing unusual in #include line
int main()
{
std::printf("Hello world\n"); // Nothing unusual in the call either
...
}
If you think the std:: part of the std::printf() call is
unusual, then the best thing to do is "get over it." In other words, it's the
standard way to use names in the standard library, so you might as well start
getting used to it now.
However if you are compiling C code using your C++ compiler, you don't want to have to tweak all these calls from printf() to std::printf(). Fortunately in this case the C code will use the old-style header <stdio.h> rather than the new-style header <cstdio>, and the magic of namespaces will take care of everything else:
/* This is C code that I'm compiling using a C++ compiler */
#include <stdio.h> /* Nothing unusual in #include line */
int main()
{
printf("Hello world\n"); /* Nothing unusual in the call either */
...
}
Final comment: if you have C headers that are not part of the standard
library, we have somewhat different guidelines for you. There are two cases:
either you can't change the
header, or you can change the
header.
|
|||