What if i want to PRINT "\n", shouldn't it be written outside the quotes to end the line rather than inside the quotes? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

27th Feb 2017, 5:38 PM
Aayush Pradhan
Aayush Pradhan - avatar
16 Answers
+ 7
no, anything you wanna print goes inside the quotes
27th Feb 2017, 6:38 PM
Norayr Gasparyan
Norayr Gasparyan - avatar
+ 1
you should put another backslash before it like cout<<"\\n"
16th Apr 2018, 9:11 AM
Hassan
Hassan - avatar
+ 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 */
16th Apr 2018, 7:38 PM
刘宇轩,一个来自西安的喜欢取长昵称的大学生。
刘宇轩,一个来自西安的喜欢取长昵称的大学生。 - avatar
+ 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');
16th Apr 2018, 7:38 PM
刘宇轩,一个来自西安的喜欢取长昵称的大学生。
刘宇轩,一个来自西安的喜欢取长昵称的大学生。 - avatar
+ 1
Hassan '&' is legal... we never use '\&'
16th Apr 2018, 8:16 PM
刘宇轩,一个来自西安的喜欢取长昵称的大学生。
刘宇轩,一个来自西安的喜欢取长昵称的大学生。 - avatar
+ 1
Hassan You can see my answer. (^_^)
16th Apr 2018, 8:44 PM
刘宇轩,一个来自西安的喜欢取长昵称的大学生。
刘宇轩,一个来自西安的喜欢取长昵称的大学生。 - avatar
+ 1
Hassan Yes...😂 Sometimes I write too much that they arent patient enough to read.
16th Apr 2018, 8:48 PM
刘宇轩,一个来自西安的喜欢取长昵称的大学生。
刘宇轩,一个来自西安的喜欢取长昵称的大学生。 - avatar
+ 1
Hassan Thank you very much...
16th Apr 2018, 8:49 PM
刘宇轩,一个来自西安的喜欢取长昵称的大学生。
刘宇轩,一个来自西安的喜欢取长昵称的大学生。 - avatar
+ 1
I am a college student of China. I love C++ very much😃
16th Apr 2018, 8:51 PM
刘宇轩,一个来自西安的喜欢取长昵称的大学生。
刘宇轩,一个来自西安的喜欢取长昵称的大学生。 - avatar
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 \" \\
16th Apr 2018, 8:11 PM
Hassan
Hassan - avatar
0
sorry i was typing something else didn't noticed thanks for pointing out 😊
16th Apr 2018, 8:40 PM
Hassan
Hassan - avatar
0
sorry i was typing something else didn't noticed thanks for pointing out 😊
16th Apr 2018, 8:41 PM
Hassan
Hassan - avatar
0
that is way long 😅😅😅
16th Apr 2018, 8:45 PM
Hassan
Hassan - avatar
0
but the content is good I read a little
16th Apr 2018, 8:48 PM
Hassan
Hassan - avatar
0
are you a student or a professional programmer
16th Apr 2018, 8:49 PM
Hassan
Hassan - avatar
0
I am a student from Pakistan same here love programming but yet I am a beginner
16th Apr 2018, 8:55 PM
Hassan
Hassan - avatar