字符串字面量定义,不允许被改变,新版本规定需要加const,不然会报错。
如果真的想修改这个字符变量,就应该把它定义为数组而不是指针。
\0也就是null,表明结束符,如果使用c语言中strlen函数进行计算,会得到\0之前的字符长度。
const char name[8] = "lh\0n";
std::cout << strlen(name) << std::endl;
const char* name1 = "lhn";
实际开发使用中基本没有1字节的char,windows是2,linux是4,所以为了普适性,使用char16、char32来强制规定。
const wchar_t* name2 = L"lhx";
const char16_t* name3 = u"lhx";
const char32_t* name4 = U"lhx";
在拼接字符串的时候大致分为两种方式:R和使用string命令空间。
using namespace std::string_literals;
std::string name5 = "lhx"s + "hello";//s是操作符函数会返回标准字符串
std::wstring name6 = L"lhx"s + L"hello";
std::u32string name7 = U"lhx"s + U"hello";
const char* example = R"(Line1
Line2
Line3
Line4)";
const char* ea = "Line1\n"
"Line2\n"
"Line3\n";