+ 2
Why writing " \ \ n " inside the cout statement, it prints \n.
5 Antworten
+ 7
I don't completely understand... Could you please try and explain the question a little better?
+ 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.
+ 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
+ 3
Since \ is an escape character it prints whatever is there after \. In this case since \n is present after \. It is printed
+ 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 :)