CS253 String Literals
constvariables are not literals. They’re variables that don’t vary.
"alpha beta gamma".
"foobar"s, with the trailing
s, but we won’t discuss it.
"string literal" is an anonymous array of characters.
These are equivalent:
cout << "abc123" << '\n';
const char make_up_a_name = "abc123"; cout << make_up_a_name << '\n';
const char make_up_a_name = "abc123"; const char *p = &make_up_a_name; cout << p << '\n';
"string literal"is like an anonymous array.
if ("beta" < "alpha") // BAD CODE cout << "This is really quite surprising!\n";
c.cc: In function 'int main()': c.cc:1:14: warning: comparison with string literal results in unspecified behavior This is really quite surprising!
"marx"two arrays or one? Who knows‽
g++ -Wallwill detect this deplorable behavior.
std::stringvalues, or to compare a
std::stringwith a C-style string, use the usual operators:
< > <= >= == !=
std::string::compare(), which has the same three-way return value as