Home  >  Article  >  Backend Development  >  C Headers in C : std:: vs. Global Namespace - Which Approach is Better?

C Headers in C : std:: vs. Global Namespace - Which Approach is Better?

Susan Sarandon
Susan SarandonOriginal
2024-11-23 07:27:10375browse

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 header assuredly provides declarations and definitions within the std namespace, while the header assuredly provides them within the global namespace.

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 headers with declarations and definitions from the global namespace, this practice is deprecated and may be removed in the future.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn