The backslash character \

Location
  1. Courses

    /

  2. Complete C++ Course

    /

  3. The basics

    /

  4. The backslash character

Quotes inside a string

In C/C++, we use quotes to define strings of character. It however creates a problem. How do we include quotes in our strings? If we put quotes inside the quotes that delimit the strings of character, then the compiler will think the string ends before it really ends (It will consider the string ends at the first quote its reaches after the beginning of the string).

Trying the following would cause a compilation error:

1
2
3
4
5
6
7
8
9
10
#include <iostream> int main() { std::string str = ""Hello""; // Error std::cout << str << std::endl; return 0; }

A way to solve that, using the class std::string, would be to add the quotes individually, using the = and += operators:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream> int main() { std::string str = '"'; str += "Hello"; str += '"'; std::cout << str << std::endl; /* '"Hello"' is printed in the console. */ return 0; }

How would we do it with an array or char? We could do it like that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream> int main() { char str[] = "-Hello-"; // We replace the first and last character by quotes. str[0] = '"'; str[6] = '"'; std::cout << str << std::endl; /* '"Hello"' is printed in the console. */ return 0; }

This is not very convenient. Is there a better way to do it? Yes: Using the backslash character \.

Using the backslash character

Quotes inside a string

We can write quotes inside the quotes delimiting a string by simply adding a backslash character \ before them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream> int main() { std::string str = "\"Hello\""; // Now it works. std::cout << str << std::endl; /* '"Hello"' is printed in the console. */ return 0; }

Apostrophe as a char

What if we want to represent an apostrophe inside a variable of type char? We use the backslash too.

char apostrophe = '\'';

How to write a backslash in a string?

To write one backslash in a string, we must write two backslashes in a row.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream> int main() { std::string str = "\\"; std::cout << str << std::endl; /* '\' is printed in the console. */ str = "\\\\"; std::cout << str << std::endl; /* '\\' is printed in the console. */ return 0; }

Representing special characters using backslashes

Until now, we have written the graphical representation of the characters in order to write them in a string (We wrote a to add the letter a to the string). But... How can we add a character that has no graphical representation, like the newline character, to a string? By using the backslash character. For example, the set of character '\n' represents a newline character (ASCII value of 10), '\t' represents a tabulation (ASCII value of 9) and '\0' represents the null character (ASCII value of 0). Therefore, the set of character '\n' can be used instead of std::endl.

The line:

std::cout << "Hello" << std::endl;

Has the same effect as:

std::cout << "Hello\n";

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream> int main() { std::cout << "\tThere is a tabulation at my left.\n"; /* The string " There is a tabulation at my left." followed by a new line is written in the console. */ char newlineChar = '\n'; // newlineChar is assigned with the number 10. std::cout << newlineChar; // Outputs a new line to the console. return 0; }