Please don't top-post.
Still confused...how does it relate to the main question-as the array
exist before and so it should print the first three characters only.
could you plz be specific
There are two types of strings in C++, the C style strings, which are
nothing more than raw arrays of char that are terminated by a '\0'
character and std::string, which has no special termination character,
because it's a class that keeps track of its size.
Whenever you deal with a C style string, the '\0' character is seen as "end
of string" character, so every function that deals with them will stop
reading at that character.
Functions that work on std::string don't do that, because '\0' is not a
special character there. They stop when they are at the character with an
index equal to length()-1.
If you write "\0Hello world", that's a string literal. You can make an
std::string from it by using the constructor that takes a const char* as
argument. However, that constructor assumes that you give it a C style
string, i.e. terminated by a '\0' character (how else would it know the
length?). Therefore, the string object will be empty, because the first
character in the C style string was '\0'.
However, if you add a single char with
someString += '\0';
to your std::string, that char is just simply appended to the string. If you
insert a character somewhere in the middle of the string with '\0' (like
you did in the original example), the length of the string won't change,
because '\0' is not treated specially. When you send that string to cout,
there is no reason to stop at the '\0'.