Home > Article > Backend Development > C Headers in C : std:: vs. Global Namespace - Which Approach is Better?
Navigating C Headers in C : std:: vs. Global Namespace
When working with C headers in C , a question arises regarding the preferred approach for calling functions: using the std:: namespace or the global namespace. While C supports the inclusion of C headers by altering the header name (e.g., stdio.h to cstdio), the semantic consideration of which approach is more appropriate remains.
The C 11 Standard addresses this issue in Section D.5 [depr.c.headers], stating that every C header (with a name format of name.h) behaves as if each name declared by the corresponding cname header is placed within the global namespace. However, it remains unspecified whether these names are declared or defined first in the std namespace and then injected into the global namespace.
For example, the
Based on this information, it is recommended to include the cname headers and utilize declarations and definitions from the std namespace. This ensures compatibility with future revisions and follows the preferred approach outlined in the standard:
#include <cstdio> int main() { std::printf("Hello world\n"); }
While it is technically acceptable to use the
The above is the detailed content of C Headers in C : std:: vs. Global Namespace - Which Approach is Better?. For more information, please follow other related articles on the PHP Chinese website!