Home > Article > Backend Development > 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:
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!