I can only find in the standard draft N4582 that
[res.on.headers/1] A C++ header may include other C++ headers.
It seems not to specify that whether a C++ header can include a C standard header.
If it is permitted, is it unsafe to use a global name defined in a C standard header even if this header is not included (since the program may implicitly include the header through some C++ standard header)?
Yes, a header can include another header. But if you are using the Arduino IDE and the headers aren't in the same folder, then you'll also need an #include line for it in the . ino file.
Including C Headers in C++ Code Note: For other than system headers, check to see whether the header was written to work with C++ compilers; that is, whether it already has linkage specifications. If so, you should not enclose the header in extern "C" brackets.
The ANSI C standard library consists of 24 C header files which can be included into a programmer's project with a single directive. Each header file contains one or more function declarations, data type definitions and macros. The contents of these header files follows.
Including a header file produces the same results as copying the header file into each source file that needs it. Such copying would be time-consuming and error-prone. With a header file, the related declarations appear in only one place.
For the purpose of the question you follow up with (name conflicts), yes, it certainly can. The reason is that the C++ standard library includes the <c:::>
C++ headers for the C standard library contents, and the standard explicitly allows these to provide the names in the global namespace as well (in addition to mandatorily providing them in ::std
).
Further, based on Annex D [depr] of the C++ standard, the C standard library headers (the <:::.h>
versions) are also part of the C++ standard library (albeit deprecated). This means [res.on.headers]/1 allows C++ headers to include them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With