0

Is there a difference between "\n" and endl in C++?

Just finished the Conditionals and Loops Unit the challenge said that my first answer was incorrect despite appearing the exact same as the expected output. But when I removed the "\n" and added endl it said it was correct.

19th Jul 2022, 8:32 PM
PPSH King
PPSH King - avatar
3 Réponses
+ 1
Yes, that can happen! endl will flush to the console immedietly when executing. '\n' on the other hand leaves it to c++ when to flush to the console. That means that the first code might have had not displayed the output fast enough.
19th Jul 2022, 10:14 PM
Erarnitox
Erarnitox - avatar
0
"\n" and endl work the same way. Are you sure you didn't make a mistake in the syntax? For example: cout<<endl; cout<<"\n"; printf("\n");
19th Jul 2022, 9:17 PM
Кавун 🍉
Кавун 🍉 - avatar