Home > Backend Development > C++ > C Headers in C : Global Namespace or std:: Namespace?

C Headers in C : Global Namespace or std:: Namespace?

DDD
Release: 2024-11-29 17:10:12
Original
358 people have browsed it

C Headers in C  : Global Namespace or std:: Namespace?

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:

  • Deprecation of "name.h" Headers: The "name.h" headers have been deprecated and may be removed in future revisions.
  • Improved Code Clarity: Using the std:: namespace helps distinguish between C and C declarations, enhancing code readability.

Example:

A preferred approach would be:

#include <cstdio>

int main() {
    std::printf("Hello world\n");
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template