+ 2

Why writing " \ \ n " inside the cout statement, it prints \n.

17th Mar 2017, 8:39 PM
cool_boy12344
cool_boy12344 - avatar
5 Respostas
+ 7
I don't completely understand... Could you please try and explain the question a little better?
17th Mar 2017, 8:41 PM
J.G.
J.G. - avatar
+ 6
For those who didnt understand,Lets take a simple example. print("Hello guys") easy enough. Lets say you were to print Hello "guys". Just try it out.You will not be able to print " because it is meant to start/end a string. To print ", you should use /".Hence to print Hello "guys" code will be print("Hello \"Guys\""); Similarly tabs can't be printed, and \ cant be as well. To print \,you will need double backslashes (\\).Hence when you write \\n, first \\ is evalutaed as \ and then n is printed as normal letters would be.
27th Mar 2017, 12:31 PM
Meharban Singh
Meharban Singh - avatar
+ 5
These are known as escape sequences. Your first backslash(\)is meant for a special keyword, which is meant for some function. Example, \n means to start a new line. \t to leave a space(tab). etc.. Hence second backslash is required to indicate the compiler to print a blackslash. Example \\ would print \ \" would print " etc. Hence \\n will output a bacjslash followed by 'n'. Google escape sequences for more knowledge
17th Mar 2017, 9:20 PM
Meharban Singh
Meharban Singh - avatar
+ 3
Since \ is an escape character it prints whatever is there after \. In this case since \n is present after \. It is printed
27th Mar 2017, 7:59 AM
Vishnu ks
Vishnu ks - avatar
+ 2
See the following code: #include<iostream> using namespace std; int main () { /* I am writing a code to print " \n" in the output screen */ cout<<" WORDs "; cout << "\n"; /* See the above line doesn't print \n on the output screen after compilation , at the time of running it just gives some space in the output screen the*/ cout << " \\n"; /* The above line does print \ n on the output screen */ return 0; } Now why this is happening ? . Why adding a " \ " results in printing of "\\n" ? Hope this might explain my Question to you :)
17th Mar 2017, 8:56 PM
cool_boy12344
cool_boy12344 - avatar