1
#include <iostream>
#include <cstring>

int main() {
    char a[]="abc";
    char b[]="def";
    std::strcpy(b,a);
    printf("%s\n", b);
    std::cout << a << std::endl;
}

why std:: is optional when using printf, while std:: cannot be omitted with cout and endl?

In /usr/include/c++/cstdio, printf is in std namespace.

namespace std {
    using ::printf;
}

But I do not understand the meaning of using ::printf. I only know using namespace namespace_name. What does :: mean? And the using clause is in a namespace, how can it work?

Tokubara
  • 392
  • 3
  • 13
  • printf comes from the C standard so you tend to have it both in the global namespace and in the std namespace (this applies to anything from the C standard). so its not that its optional for printf, its that printf is in 'both places' – Borgleader Apr 02 '21 at 15:18
  • 2
    See __C compatibility headers__ in https://en.cppreference.com/w/cpp/header _"...and the corresponding cxxx headers are allowed to also declare the same names in the global namespace..."_ – Richard Critten Apr 02 '21 at 15:19
  • 2
    Voted to reopen because the duplicate does not address the question. – anastaciu Apr 02 '21 at 15:19
  • 1
    agreed the duplicate is incorrect – Borgleader Apr 02 '21 at 15:36
  • I applied [a new duplicate](https://stackoverflow.com/q/18047031/364696); it's not *exact* (it's specifically asking about `ispunct`), but it's the same basic question: "Why are some (unbeknownst to asker) C APIs not required to use `std::` prefixes?" with the same answer. – ShadowRanger Apr 02 '21 at 15:52
  • The duplicate does answer one part of the question. I believe [this](https://stackoverflow.com/questions/22397894/c-using-a-symbol-from-a-namespace) should cover the other part. – IWonderWhatThisAPIDoes Apr 02 '21 at 15:56

0 Answers0