"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Why does `printf` work in both `std::printf` and `printf` when using `` in C++?

Why does `printf` work in both `std::printf` and `printf` when using `` in C++?

Published on 2024-12-21
Browse:667

Why does `printf` work in both `std::printf` and `printf` when using `` in C  ?

cstdio Stdio.h Namespace

In the C reference documentation for , it claims that all library elements reside within the std namespace. However, experiments show that both std::printf and printf function calls work. Does this indicate that C headers import symbol names into both the std and global namespaces?

Answer

Including imports symbol names into the std namespace, and potentially the global namespace. In contrast, including imports symbol names into the global namespace, potentially incorporating them into the std namespace as well.

This behavior extends to all C-style headers. According to the C 11 standard (Annex D), C standard library headers behave as follows:

"2 Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace scope."

Therefore, it remains uncertain whether these names are initially declared or defined within the namespace std and then injected into the global namespace via using-declarations, or vice versa.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3