Using C Headers in C : Global Namespace or std:: Namespace?
In C , the use of C headers is possible with minor modifications to their names, for example, stdio.h becomes cstdio. This raises the question of whether it is preferable to use functions from the std:: namespace or the global namespace when calling C functions in C code.
Explanation:
According to the C 11 Standard, all C header names in the form of "name.h" are automatically placed in the global namespace for compatibility with the C standard library. However, the Standard does not specify whether these names are first declared or defined within the std:: namespace and then injected into the global namespace or vice versa.
Recommendation:
Based on the Standard's recommendations, it is suggested to prefer including the "cname" headers (e.g., cstdio) and using declarations and definitions from the std:: namespace for the following reasons:
Example:
A preferred approach would be:
#include <cstdio> int main() { std::printf("Hello world\n"); }
Conclusion:
While it is possible to call C functions using either the global namespace or the std:: namespace, it is more advisable to follow the suggestions of the C Standard and prefer using cname headers with std:: namespace declarations and definitions.
The above is the detailed content of C Headers in C : Global Namespace or std:: Namespace?. For more information, please follow other related articles on the PHP Chinese website!