I'm testing an example about strings in C++ from "C++ Premiere" book.
const int size = 9;
char name1[size];
char name2[size] = "C++owboy"; // 8 characters here
cout << "Howdy! I'm " << name2 << "! What's your name?" << endl;
cin >> name1; // I input "Qwertyuiop" - 11 chars. It is more than the size of name1 array;
// now I do cout
cout << "Well, your name has " << strlen(name1) << " letters"; // "Your name has 11 letters".
cout << " and is stored in an array of " << size(name1) << " bytes"; // ...stored in an array of 9 bytes.
How it can be that 11 chars are stored in an array just for 8 chars + '\0' char? Is it becomes wider on compilation? Or the string is stored somewhere else?
Also, I can't do:
const int size = 9;
char name2[size] = "C++owboy_12345"; // assign 14 characters to 9 chars array
But can do what I've written above:
cin >> name1; // any length string into an array of smaller size
What is the trick here? I use NetBeans and Cygwin g++ compiler.