For s you correspond to a constructor that accepts a list of character initializers: this is (9) in the list here . The string class allows you to create strings from arbitrary data, which can include embedded NULs, as in this case. The initialization list knows its own length, so string captures all characters.
For s1 the matching constructor is (5) in the above list, which takes const char* - the compiler allows the char array to provide decay to such a pointer before calling this constructor, which means that the constructor does not know the length of the array. Instead, it is assumed that you intentionally use the ASCIIZ NUL-terminated convention (as in the "C" lines) and scan the data to find the first NUL, given that the terminator is. Therefore, the line contains only 2 characters.
Please note that you can explicitly write 4 characters with ...
std::string s1 { "ab\0c", 4};
... that matches the constructor (4) in the list.
The Rakete1111 comment below illustrates another, newer way of creating such strings: auto s1 = "ab\0c"s; .
source share