Home > Backend Development > C++ > body text

Why does cstdio import symbols into the std namespace while stdio.h imports them into the global namespace?

DDD
Release: 2024-10-31 02:01:29
Original
583 people have browsed it

Why does cstdio import symbols into the std namespace while stdio.h imports them into the global namespace?

cstdio stdio.h Namespace

In C , the standard C library is available within the std namespace. However, a common misconception arises when using headers such as cstdio and stdio.h.

Including cstdio imports the symbol names into the std namespace. However, including stdio.h imports the symbol names into the global namespace. This is because C headers, including cstdio, behave as if the names placed in the std namespace are also placed in the global namespace.

Example:

The following code uses the std::printf function, which is defined within the std namespace:

<code class="cpp">std::printf("hello world"); // Correct</code>
Copy after login

In contrast, the following code uses the printf function, which is defined in the global namespace:

<code class="cpp">printf("hello world"); // Correct</code>
Copy after login

Note that including cstdio may also make the printf function available in the std namespace, but this is not guaranteed. To ensure availability in both namespaces, it is recommended to use the header corresponding to the desired namespace:

  • cstdio - Names defined in std namespace
  • stdio.h - Names defined in global namespace

The above is the detailed content of Why does cstdio import symbols into the std namespace while stdio.h imports them into the global 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!