Correct use of \n | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Correct use of \n

The questions use new line like " cout << "Hello \n World"; Not sure if that would work on the computer. Shouldnt it be " cout << "Hello " << \n << "world!";

2nd Jul 2019, 3:44 AM
Johnny Cruz Martinez
Johnny Cruz Martinez - avatar
9 Answers
+ 2
It will definitely work the correct way is \n has to be inside " " doesn't matter if it is in the middle of the sentence like cout<<"Hell\no world"; will print Hell o world
2nd Jul 2019, 1:39 PM
Vratika Nagda
Vratika Nagda - avatar
+ 6
The correct of \n is following cout << "Hello \n World"; In line cout << "Hello " << \n << "world!"; The problem is you have to pass "\n" with the double quotes. This line will work if you will put \n inside quotes. Like this👇 cout << "Hello " << "\n "<< "world!";
2nd Jul 2019, 3:53 AM
Deepak Kumar
Deepak Kumar - avatar
+ 6
endl is another way to add a new line.
2nd Jul 2019, 5:00 AM
Sonic
Sonic - avatar
+ 3
These are known as EscapeSequences. •\t: It is used forHorizontal tab. •\n: It is used forNew line. •\b: It is used forBackspace. Hope it was helpful..!
2nd Jul 2019, 10:24 AM
Saurav Kashyap
Saurav Kashyap - avatar
+ 2
Yes it's best Sonic
2nd Jul 2019, 2:06 PM
Charan Leo25
Charan Leo25 - avatar
+ 2
both will work fine just remember to use single quotes while using escape sequence
3rd Jul 2019, 6:12 AM
Aditya
Aditya - avatar
+ 1
Using it inside the string (double quotes) is the correct way. There are other characters that are enterpretted too like this, all starting with the “\” like “\t” for a tab. The “\” is used to escape the string too for characters that would normally cause problems, like double quotes. cout << “He said \”Hello world\” and others knew he was a programmer” << endl; This line prints quotes around “Hello world” without breaking the string.
2nd Jul 2019, 3:13 PM
nk361
nk361 - avatar
+ 1
Both are correct 🙂
3rd Jul 2019, 5:42 AM
Sanjay Kamath
Sanjay Kamath - avatar
0
Hay
2nd Jul 2019, 1:48 PM
Arif Marsmello