Raw string literal in C++ program


In this article, we will be discussing the raw string literal in C++, its meaning, and examples.

There are escape characters in C++ like “\n” or “\t”. When we try to print the escape characters, it will not display on the output. To show the escape characters on the output screen we use a raw string literal by using R”(String with escape characters)”. After using R in the front of the string the escape character will be displayed on the output.

Example

Let’s understand this with the help of an example

 Live Demo

#include <iostream>
using namespace std;
int main(){
   string str = "tutorials\npoint\n" ;
   // A Raw string
   string str_R = R"(tutorials\npoint\n)";
   cout <<"String is: "<<str << endl;
   cout <<"Raw String is: "<<str_R;
   return 0;
}

Output

If we run the above code it will generate the following output −

String is: tutorials
point
Raw String is: tutorials\npoint\n

Example

 Live Demo

#include <iostream>
using namespace std;
int main(){
   string str = "tutorials\ttoint\t" ;
   // A Raw string
   string str_R = R"(tutorials\tpoint\t)";
   cout <<"String is: "<<str << endl;
   cout <<"Raw String is: "<<str_R;
   return 0;
}

Output

If we run the above code it will generate the following output −

String is: tutorials toint
Raw String is: tutorials\tpoint\t

Updated on: 22-Apr-2020

455 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements