+ 1
What if i want to PRINT "\n", shouldn't it be written outside the quotes to end the line rather than inside the quotes?
16 Answers
+ 7
no, anything you wanna print goes inside the quotes
+ 1
you should put another backslash before it
like
cout<<"\\n"
+ 1
/* Part #1 */
The escape character '\\' (or '\134' or '\x5c') means the backslash(\), and 'n' (or '\156' or '\x6e') means the letter n.
So if you wants to print the two letters, you can use any combination, for exapmle:
std::cout<<"\\n"; // It's the most commonly used.
std::cout<<"\\\156";
std::cout<<"\\\x6e";
std::cout<<"\134n";
std::cout<<"\134\156";
std::cout<<"\134\x6e";
std::cout<<"\x5cn";
std::cout<<"\x5c\156";
std::cout<<"\x5c\x6e";
There are many characters that you cannot print directly, such as new line, so we use escape sequences. When we see a backslash, we need know that the character(s) following with it will have a different meaning, for example:
'\a' means an alert (or beep or bell)
'\b' means a backspace
'\f' means a formfeed
'\n' means a newline (or line feed)
'\r' means a carriage return
'\t' means a horizontal tab
'\v' means a vertical tab
'\\' means a backslash
'\'' means a single quotation mark
'\"' means a double quotation mark
'\?' means a question mark
/* To Be Continued */
+ 1
/* Answer Part #2 */
(Note: '\?' is the same as '?' , but, '\?' can avoid trigraphs like "??=". In some computers, it will automatically convert to "#". So, if you use 2 or more question marks together, please use something like "?\?\?\?" instead of "????".)
'\n' (or '\012' or '\x0a') means the new line, so if you wants to print in a new line, you can use:
std::cout<<"\n"; // It's commonly used.
std::cout<<'\n';
std::cout<<"\012";
std::cout<<'\012';
std::cout<<"\x0a";
std::cout<<'\x0a';
Since it is just one character, you can use both single quotes' ' (it means one character) and double quotes" " (it means a character array.)(Note:"\n" actually has 2 characters: '\n' and '\0', which means the end of the character array).
There is two more eays to print in a new line.
std::cout<<std::endl; // It's the most commonly used.
std::endl(std::cout);
endl is a manipulator, so it can be used as a function as well as a parameter of operator<< .
However, endl isn't the same as '\n' , there is a little difference --- endl will flush the buffer, while '\n' won't, so, the following statements are equal.
std::cout<<std::endl;
std::cout<<'\n'<<std::flush;
std::endl(std::cout);
std::flush(std::cout<<'\n');
+ 1
Hassan '&' is legal... we never use '\&'
+ 1
Hassan You can see my answer. (^_^)
+ 1
Hassan Yes...😂 Sometimes I write too much that they arent patient enough to read.
+ 1
Hassan Thank you very much...
+ 1
I am a college student of China. I love C++ very much😃
0
whenever you want to print an escape sequence such as \n or \t or some special characters (such as " or & etc ) that can't be printed inside double quotes you put a \ before that escape sequence or special character
such as\\n \\t \\a \" \\
0
sorry i was typing something else didn't noticed
thanks for pointing out 😊
0
sorry i was typing something else didn't noticed
thanks for pointing out 😊
0
that is way long 😅😅😅
0
but the content is good I read a little
0
are you a student or a professional programmer
0
I am a student from Pakistan same here love programming
but yet I am a beginner