"Using" question

The following code is a slightly modified version of an example in a beginner's C++ book:

#include <iostream>
#include <cstring>
int main()
{
using std::cout;
using std::endl;

char string1[] = "No man is an island";
char string2[80] = {'\0'};

strcpy(string2, string1);

cout << "String1: " << string1 << endl;
cout << "String2: " << string2 << endl;

return(0);
}

My question is, why do I NOT need a "using std::strcpy" statement? I'm running Windows 10, using Codeblocks as my IDE, and the GNU compiler. (The original program had "using namespace std" instead of the two "using" statements, perhaps so that the author wouldn't have to answer this question!)
It is probably because the C-string functions are in both the std::namespace and in the global namespace.

By the way a bigger question is why are you using C-strings instead of C++ strings?

closed account (E0p9LyTq)
By the way a bigger question is why are you using C-strings instead of C++ strings?

In a way that question is already answered:

a beginner's C++ book

Most beginner's C++ books are badly written and/or outdated (pre-C++11 or even pre-C++03), with a lot of C code all over the place.

(The original program had "using namespace std" instead of the two "using" statements

No, more likely the author is lazy, relying on outdated usages and not wanting to keep retyping "std::whatever".

C++ has evolved and changed considerably with C++11/C++14/C++17. Most C++ books, even with recent copyright dates are still years behind the newer standards.

The sorry state of outdated book documentation can make learning a challenge.
If you include the C string header as <cstring> then you should use std:: in front of the functions since they will definitely be declared there. But an implementation is allowed, though not required, to dump the declarations into the global namespace as well. But if you don't want to put std:: in front of C functions, you should include the header as <string.h> which ensures that the functions will be declared in the global namespace (although if I remember correctly they are allowed to be put into std::, too!).
Last edited on
Thanks for everyone's reply; I learned a lot about the global namespace! I think that the example in the book using C-strings was put there because there's a lot of code out there that still uses them; I've read on the net that there are coders out there that loath OOP in general and C++ in particular. The next section of my book covered C++ string classes, which seem to me to be much easier to use.
Topic archived. No new replies allowed.