Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a C++ standard library header include a C standard header?

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)?

like image 982
xskxzr Avatar asked Jun 02 '16 10:06

xskxzr


People also ask

Can you include a header file in another header file C?

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.

Can I include C++ header in C 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.

How many header files make up the standard C library?

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.

What happens when you include a header file in C?

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.


1 Answers

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.

like image 172
Angew is no longer proud of SO Avatar answered Sep 26 '22 06:09

Angew is no longer proud of SO