Using Special Characters in Strings

Special characters can be included in strings using escape sequences. These characters represent actions like newline, tab, or quotation marks.

Common Escape Sequences

  • \n - Newline
  • \t - Tab
  • \\ - Backslash
  • \" - Double Quote
  • \' - Single Quote

Example: Using Escape Sequences

#include <iostream>

int main() {
    std::cout << "She said, \"Hello, World!\"" << std::endl;
    std::cout << "Line1\nLine2\nLine3" << std::endl;
    return 0;
}

Output:

She said, "Hello, World!"

Line1

Line2

Line3

Key Takeaways

  • Escape sequences allow special characters to be included in strings.
  • They are prefixed with a backslash (\).
  • Useful for formatting output and including special symbols.