CS253: Software Development with C++

Spring 2018

String Literals

See this page as a slide show

CS253 String Literals

Literals

String Literals

A "string literal" is an anonymous array of constant characters. These are equivalent:

cout << "FN-2187";
FN-2187
const char whatever[] = "FN-2187";
cout << whatever;
FN-2187
const char whatever[] = "FN-2187";
const char *p = &whatever[0];
cout << p;
FN-2187

Comparing C-Style Strings

if ("foo" < "bar")
    cout << "😢";
c.cc:1: warning: comparison with string literal results in unspecified behavior
😢

Comparing C-style strings properly.

Comparing C++ std::strings

    < > <= >= == !=

Example

string name = "Conan O’Brien";
if (name == "Conan O’Brien")
    cout << "good 1\n";
if (name < "Zulu")
    cout << "good 2\n";
if (name > "Andy Richter")
    cout << "good 3\n";
if (name == name)
    cout << "good 4\n";
good 1
good 2
good 3
good 4

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-04-24T16:56

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building