Home  >  Article  >  Backend Development  >  Why does cstdio import symbols into the std namespace while stdio.h imports them into the global namespace?

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

DDD
DDDOriginal
2024-10-31 02:01:29583browse

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>

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

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

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!

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