I initialized a C++ string with a string literal and replaced a char with NULL.
When printed with cout <<
the full string is printed and the NULL char prints as blank.
When printed as c_str
the string print stop at the NULL char as expected.
I'm a little confused. Does the action came from cout
? or string
?
int main(){
std::string a("ab0cd");
a[2] = '\0'; // '\0' is null char
std::cout << a << std::endl; // abcd
std::cout << a.c_str() << std::endl; // ab
}
Test it online.
I'm not sure whether the environment is related, anyway, I work with VSCode in Windows 10